IOS-如何将视频添加为 ViewController 背景并使用 swift 在运行时添加约束

标签 ios iphone video swift viewcontroller

我想为我的 iPhone 应用程序创建一个具有视频背景的登录屏幕。 查看 this sample 以获得概念引用。

Q:有没有类似UIImageView的可以播放视频的? 什么是正确的方法?

编辑

我已设法添加视频并对其进行缩放以适合 View ,但是当我旋转设备时,视频被裁剪了。

代码:

override func viewDidLoad() {
    super.viewDidLoad()

    var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")!

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

    moviePlayer.controlStyle = MPMovieControlStyle.None
    moviePlayer.scalingMode = MPMovieScalingMode.AspectFill

    self.view.insertSubview(moviePlayer.view, atIndex: 0)
    moviePlayer.play()

}

尝试在运行时添加约束但失败。

如何添加约束以在设备旋转时保持全屏 View ?

最佳答案

我添加了以下约束并且它起作用了。只需让 moviePlayer View 居中,并且它的宽度和高度等于它的父 View 的宽度和高度。

theView.setTranslatesAutoresizingMaskIntoConstraints(false)

var constX = NSLayoutConstraint(item: theView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
view.addConstraint(constX)

var constY = NSLayoutConstraint(item: theView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
view.addConstraint(constY)

var constW = NSLayoutConstraint(item: theView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
view.addConstraint(constW)

var constH = NSLayoutConstraint(item: theView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
view.addConstraint(constH)

关于IOS-如何将视频添加为 ViewController 背景并使用 swift 在运行时添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27589463/

相关文章:

iphone - 如何给 UILabel 添加效果

video - 添加覆盖时ffmpeg中的丢帧

iOS11 在所有移动浏览器中导致 CORS 问题

ios - GCM 注册未准备好使用 ios9 中的身份验证凭据

ios - 构建自定义 UIPickerView

html - 仅在 Safari 中背景视频位置不正确

c# - 统一: The audio of a video file is choppy when played with Unity

ios - 如何使用 facebook SDK 检查 iOS 中是否安装了 fbmessanger

iphone - 带有 AVPlayer 的音频流

iPhone:将信息从网页传递到从 App Store 下载的应用程序