在Swift中加载带透明背景的MPMoviePlayerViewController?

时间:2015-01-24 04:09:21

标签: ios swift uiview mpmovieplayercontroller

我正在使用Swift(iOS 8.1)中的MPMoviePlayerViewController,并按照How to play a local video with Swift?的答案使用代码,这一切都正常。

此代码如下:

    let path = NSBundle.mainBundle().pathForResource("movie",     ofType:"m4v")
    let url = NSURL.fileURLWithPath(path!)
    moviePlayer = MPMoviePlayerController(contentURL: url)

    if let player = moviePlayer {
        player.view.frame = self.animationImage.frame
        player.backgroundView.backgroundColor = UIColor.clearColor()
        player.view.backgroundColor = UIColor.clearColor()

        for subView in player.view.subviews  {
            subView.backgroundColor = UIColor.clearColor()
        }

        player.prepareToPlay()
        player.scalingMode = .AspectFill
        player.controlStyle = .None
        self.view.addSubview(player.view)
    }

但我试图让电影播放器​​的背景透明化。我找到了使用Objective-C的答案,但我在编译时遇到了错误。我目前的代码是:

player.backgroundView.backgroundColor = UIColor.clearColor()
player.view.backgroundColor = UIColor.clearColor()

for subView in player.view.subviews  {
     subView.backgroundColor = UIColor.clearColor()
} 

在subView.backgroundColor = UIColor.clearColor()行上发生编译错误。错误是:

' @lvalue $ T4'与' CGColor !!'

不同

对于我在这里做错的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:3)

我认为您可能希望指定subView的类型为UIView

for subView in moviePlayer!.view.subviews as [UIView] {
    subView.backgroundColor = UIColor.clearColor()
}