Ios 自动布局-如何添加子视图以匹配UIView的大小?

Ios 自动布局-如何添加子视图以匹配UIView的大小?,ios,swift,autolayout,Ios,Swift,Autolayout,我被困在一个问题上,我遇到了自动布局。 我有一个ui视图,它设置了约束,因此对于每个尺寸等级,它都处于相同的位置。我正试图让我的视频完全适合这个UIViewvideoViewBox。我只在iPhone6上使用它,但是如果我切换到iPhone4S/5,视频就不再对齐了 代码如下: var moviePlayer: MPMoviePlayerController! @IBOutlet var videoViewBox: UIView! override func view

我被困在一个问题上,我遇到了自动布局。 我有一个
ui视图
,它设置了约束,因此对于每个尺寸等级,它都处于相同的位置。我正试图让我的视频完全适合这个
UIView
videoViewBox
。我只在iPhone6上使用它,但是如果我切换到iPhone4S/5,视频就不再对齐了

代码如下:

    var moviePlayer: MPMoviePlayerController!

    @IBOutlet var videoViewBox: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let path = NSBundle.mainBundle().pathForResource("tutorial", ofType: "mov")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        moviePlayer.view.frame = CGRect(x: 10, y: 50, width: 255, height: 400)
        videoViewBox.addSubview(moviePlayer.view)

        moviePlayer.fullscreen = true
        moviePlayer.controlStyle = MPMovieControlStyle.None
    }
我尝试使用
videoViewBox.frame.origin.x
videoViewBox.frame.origin.y
中的值动态设置
CGRect
的值,但它也没有对齐


我该怎么做呢?谢谢你的帮助

不要为电影播放器视图设置帧。相反,添加自动布局约束以将moviePlayer视图约束到其superview。您可以使用可视格式执行此操作:

moviePlayer.view.translatesAutoresizingMaskIntoConstraints = false
videoViewBox.addSubview(moviePlayer.view)

let views = ["moviePlayerView": moviePlayer.view]

let hconstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[moviePlayerView]|", metrics: nil, views: views)
NSLayoutConstraint.activate(hconstraints)

let vconstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[moviePlayerView]|", metrics: nil, views: views)
NSLayoutConstraint.activate(vconstraints)

或者,您可以使用锚定:

moviePlayer.view.translatesAutoresizingMaskIntoConstraints = false
videoViewBox.addSubview(moviePlayer.view)

moviePlayer.view.topAnchor.constraint(equalTo: videoViewBox.topAnchor).isActive = true
moviePlayer.view.bottomAnchor.constraint(equalTo: videoViewBox.bottomAnchor).isActive = true
moviePlayer.view.leadingAnchor.constraint(equalTo: videoViewBox.leadingAnchor).isActive = true
moviePlayer.view.trailingAnchor.constraint(equalTo: videoViewBox.trailingAnchor).isActive = true

您需要使用AutoLayoutConstraints来实现这一点

您可以将这两种约束直接应用于故事板或通过代码。我假设你想要的是通过代码来完成。在这种情况下,应该这样做:

let path = NSBundle.mainBundle().pathForResource("tutorial", ofType: "mov")
let url = NSURL.fileURLWithPath(path!)

moviePlayer = MPMoviePlayerController(contentURL: url)
let movieView = moviePlayer.view!

//this will be the height of your control
var heightValue: CGFloat = 400

//This will leave a marging of 50 on top
var top = NSLayoutConstraint(item: movieView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.GreaterThanOrEqual, toItem: videoViewBox, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 50 )
//This will be the height of the control
var height = NSLayoutConstraint(item: movieView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: heightValue )
//This will leave a marging of 10 at the end
var trailing = NSLayoutConstraint(item: movieView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: videoViewBox, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 10)
//This will leave a marging of 10 at the start
var leading = NSLayoutConstraint(item: movieView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: videoViewBox, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 10)

movieView.setTranslatesAutoresizingMaskIntoConstraints(false)

NSLayoutConstraint.activateConstraints([top, height, trailing, leading])
    videoViewBox.addSubview(movieView)

moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.None

非常感谢,比我想象的要简单!