2016-09-22 35 views
0

在我的故事板中,我在UIViewController中放置了一個視圖。 我添加必要的參數有:[AVPlayerLayer superview]:在Swift中發送到實例的無法識別的選擇器

enter image description here

,並與我的代碼連接它:

enter image description here

在代碼中,我有:

,並導致錯誤:

*** Terminating app due to uncaught exception 
'NSInvalidArgumentException', reason: '-[AVPlayerLayer superview]: 
unrecognized selector sent to instance 0x7fe320cb2030' 

你能告訴我是什麼原因導致了這個問題,它如何解決?

回答

4

A UIView不是CALayer,您不能將AVPlayerLayer添加到故事板。

取而代之,在視圖加載後,以編程方式將AVPlayer的playerLayer添加到UIView的圖層。

更改您的出口是一個UIView

@IBOutlet weak var yawpVideo: UIView! 

然後,添加圖層:

player = AVPlayer(URL: url) 
let playerLayer = AVPlayerLayer(player: player) 
yawpVideo.layer.addSublayer(playerLayer) 
+0

好,感謝的人,它的工作,我也不得不把一些約束編程:'playerLayer .frame = CGRect(x:0,y:0,width:self.view.frame.size.width,height:self.view.frame.size.height)',現在視頻位於屏幕的中心 - 我怎樣才能讓它從屏幕的最頂端開始? – user3766930

+0

@ user3766930那些不是約束,你手動設置圖層的框架。如果您想要將播放器圖層更改爲屏幕中心,請將約束添加到故事板中的「yawpVideo」視圖。 – JAL

相關問題