AVPlayerViewController 的视图作为子视图

AVPlayerViewController's view as subview

我正在尝试这样配置 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 视图嵌套在另一个视图中的正确方法吗?

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

[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 controller containment is more than just adding a view controller's view to your hierarchy

使用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];
    }