Autolayout and AVPlayerViewController

643 views Asked by At

I am adding a AVPlayerViewController instance to a UIView. The UIView is initialized through the storyboard. I set the top constraint to the top layout and the bottom constraint to the bottom layout. When the app is compiled, the view is the right size but shifted 64px down (height of top nav bar). How do I solve this? Setting top constraint to 64 solves the problem for a bit, until I go back in the views and then it still becomes misaligned.

//AVPlayer object can direct its visual output to AVPlayer. AVPlayerVC is a AVPlayerViewController. You can add it via objects in bottom-right corner.
AVPlayerViewController *avPlayerVC=[[AVPlayerViewController alloc] init];
avPlayerVC.player=self.avPlayer;

[self addChildViewController:self.avPlayerVC];
[self.playerView addSubview:self.avPlayerVC.view];
self.avPlayerVC.view.frame = self.playerView.frame;

[avPlayerVC didMoveToParentViewController:self];

DLog(@"%@",NSStringFromCGRect(avPlayerVC.view.frame));
DLog(@"%@",NSStringFromCGRect(self.playerView.frame));
DLog(@"%@",NSStringFromCGRect(self.view.frame));

[self.avPlayerVC.player play];

enter image description here

enter image description here

Log:

[Line 95] {{0, 0}, {100, 100}}
[Line 97] {{0, 64}, {375, 554}}
[Line 99] {{0, 0}, {375, 667}}
1

There are 1 answers

0
iTamilan On

When constraints get updated viewDidLayoutSubviews method will call. Set the bounds of player view as avplayer frame.

-(void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.avplayer.frame setFrame:self.playerView.bounds]; }