简体   繁体   English

横向,半视角。 迅速

[英]Landscape orientation, half view. Swift

My app works fine in portrait orientation but when i turn to landscape mode I see white square trough half view. 我的应用在纵向方向上运行良好,但是当我转到横向模式时,我看到了白色方形槽的一半视图。

http://screenshot.cz/22XZB/sss.png

What's the problem? 有什么问题?

Code of movie player 电影播放器​​代码

    moviePlayer = MPMoviePlayerController(contentURL: stream)
    moviePlayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    moviePlayer.view.sizeToFit()
    self.view.addSubview(moviePlayer.view)
    moviePlayer.fullscreen = true
    moviePlayer.controlStyle = MPMovieControlStyle.Embedded
}

You need to resize moviePlayer when its superview's bounds change. 当其moviePlayer视图的边界发生变化时,您需要调整moviePlayer大小。

One solution is to move this line: 一种解决方案是移动此行:

moviePlayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)

Into viewWillLayoutSubviews (if this is a view controller subclass) or layoutSubviews (if this is a view subclass). 进入viewWillLayoutSubviews (如果这是视图控制器子类)或layoutSubviews (如果这是视图子类)。

Another is to use auto-layout instead of manual layout. 另一种是使用自动布局而不是手动布局。

As you are not using autolayout , you must update the view's frame when orientation is done, to do so you can use the method 由于您没有使用autolayout ,因此必须在定向完成后更新视图的框架,为此您可以使用方法

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

}

This methods is called every time when a orientation change is done and here you can update or set the frame of view to your needs. 每次更改方向后都会调用此方法,您可以在此处根据需要更新或设置视域。

Another way is to use Autolayout which is much effective. 另一种方法是使用自动布局,它非常有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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