3

我正在尝试这样配置AVPlayerViewController

- (void) willMoveToSuperview:(UIView *)newSuperview我称之为代码:

if(self.moviePlayerController == nil) {

                self.moviePlayerController = [[AVPlayerViewController alloc] init];

                [self.view addSubview: [self.moviePlayerController view]];

                [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
                [self.moviePlayerController.view setTranslatesAutoresizingMaskIntoConstraints:NO];

                NSDictionary *views = @{ @"selfview" : self.view, @"movieControllerView" : [self moviePlayerController].view};

                [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[movieControllerView]|"
                                                                                  options:0
                                                                                  metrics:nil
                                                                                    views:views]];
                [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[movieControllerView]|"
                                                                                  options:0
                                                                                  metrics:nil
                                                                                    views:views]];
            }

但是当创建播放器时,我得到了这个异常:

Unable to simultaneously satisfy constraints.
                Probably at least one of the constraints in the following list is one you don't want.
                Try this:
                (1) look at each constraint and try to figure out which you don't expect;
                (2) find the code that added the unwanted constraint or constraints and fix it.
                (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
                (
                 "<NSAutoresizingMaskLayoutConstraint:0x1499d1730 AVPlayerView:0x149885150.(null) == TSNView:0x14996bca0.(null) - 110>",
                 "<NSLayoutConstraint:0x1498bf480 AVPlayerView:0x149885150.leading == TSNView:0x14996bca0.leading>",
                 "<NSLayoutConstraint:0x1498bc800 UIView:0x14996bca0.trailing == AVPlayerView:0x149885150.trailing>"
                 )

                Will attempt to recover by breaking constraint
                <NSLayoutConstraint:0x1498bc800 UIView:0x14996bca0.trailing == AVPlayerView:0x149885150.trailing>

该代码可以正常工作MPMoviePlayerController。这是我可以AVPlayerViewController's使用自动布局将视图嵌套在另一个视图中的正确方法吗?

4

2 回答 2

4

在将视图添加到层​​次结构中时,自动调整大小掩码被转换为约束。因此,您应该更改此顺序:

[self.view addSubview: [self.moviePlayerController view]];
[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.moviePlayerController.view setTranslatesAutoresizingMaskIntoConstraints:NO];

进入这个:

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.moviePlayerController.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview: [self.moviePlayerController view]];

另外,我希望您了解视图控制器的包含不仅仅是将视图控制器添加view到您的层次结构中。

于 2015-11-30T13:12:01.947 回答
0

使用KVConstraintExtensionsMaster库以编程方式应用约束。

 if( self.moviePlayerController == nil)
    {
        self.moviePlayerController = [[AVPlayerViewController alloc] init];
        [self.view addSubview: [self.moviePlayerController view]];  
        /* prepare moviePlayerController view for autolayout constraint */
        [self.moviePlayerController.view prepareViewForAutoLayout];
        /* apply constraint to fit superView (self.view) */
        [self.moviePlayerController.view applyConstraintFitToSuperview];
    }
于 2015-12-01T21:15:24.610 回答