简体   繁体   中英

Autolayout and AVPlayerViewController

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

在此处输入图片说明

在此处输入图片说明

Log:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM