AVPlayerViewController's view as subview

I am trying to configure AVPlayerViewController this way:

In - (void) willMoveToSuperview:(UIView *)newSuperview I call this code:

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

But when the player is created I get this exception:

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>

The code was working for MPMoviePlayerController without an issue. Is that a proper way how I can nest the AVPlayerViewController's view inside another view using auto layout?

Upvotes: 3

Views: 2277

Answers (2)

keshav vishwkarma
keshav vishwkarma

Reputation: 1852

Apply programmatically constraint by using theKVConstraintExtensionsMaster library.

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

Upvotes: 0

Tam&#225;s Zahola
Tam&#225;s Zahola

Reputation: 9321

The autoresizing mask is translated into constraints upon adding the view into the hierarchy. Therefore you should change the order of this:

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

Into this:

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

Also I hope you understand that view controller containment is more than just adding a view controller's view to your hierarchy.

Upvotes: 4

Related Questions