AVPlayerViewController的视图为子视图

时间:2015-11-30 12:24:48

标签: ios objective-c autolayout nslayoutconstraint avplayerviewcontroller

我正在尝试以这种方式配置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视图嵌套在另一个视图中吗?

2 个答案:

答案 0 :(得分: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 controller containmentmore than just adding a view controller's view to your hierarchy

答案 1 :(得分: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];
    }