AVPlayerViewController的视图作为子视图

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的视图嵌套在另一个视图中?


当您在XCode中查看可以拖入故事板的对象时:在底部找到一个容器视图。那是一个可以包含控制器的视图。将整个moviePlayerController放置在该视图内,而不仅仅是其视图。 - Gerd Castan
@GerdCastan 我只是在使用代码,但我会尝试一下。谢谢。编辑:我正在使用简单的UIView,在那里我需要放置控制器,这是不可能的。 - Vladimír Slavík
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”添加到您的层次结构中。参考链接:视图控制器容器实现一个容器视图控制器

这个可以工作,但我不理解那个。它应该在setNeedsLayout或layoutIfNeeded之后进行处理。而且我期望的顺序不应该有任何影响。但实际上确实有影响。谢谢@Tamas。 - Vladimír Slavík
1
translatesAutoresizingMaskIntoConstraints 会创建实际的 NSAutoresizingMaskLayoutConstraint 并将它们添加到父视图中。这就是为什么在视图被添加后关闭它是徒劳无功的,因为约束已经存在。 - Tamás Zahola

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接