2017-08-26 89 views
3

我試圖以編程方式將視頻約束到頁面的中心。我的影音控制器被稱爲avPlayerController無法以編程方式向視頻添加約束

我已經與寬度和高度上沿着給予其x和y的值:

avPlayerController.view.frame = CGRect(x: 36 , y: 20, width: 343, height: 264)

讓我怎麼中心呢?

我曾嘗試:Programmatically Add CenterX/CenterY Constraints

但是,你能猜出它沒有工作:(

這裏是我的代碼:

super.viewDidLoad() 
     let filepath: String? = Bundle.main.path(forResource: "rockline", ofType: "mp4") 
     let fileURL = URL.init(fileURLWithPath: filepath!) 


     avPlayer = AVPlayer(url: fileURL) 


     let avPlayerController = AVPlayerViewController() 
     avPlayerController.player = avPlayer 
     avPlayerController.view.frame = CGRect(x: 36 , y: 20, width: 343, height: 264) 

     // hide/show control 
     avPlayerController.showsPlaybackControls = false 
     // play video 

     avPlayerController.player?.play() 
     self.view.addSubview(avPlayerController.view) 
    avPlayerController.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 
+0

設置框架與設置約束不一樣。佈局實際上是在viewDidLayoutSubviews中完成的 – Pruthvikar

+2

這並不是說你問很多問題,而是刪除問題並重新提問!兩個小時前,你問這個,像@Pruthvikar,我評論說你不應該設置框架。我還詳細說明了要做的三件事情,並且*您*評論了要求提供的代碼。我可以那樣做,但是你也會刪除這個問題嗎? – dfd

+0

所以對不起我我承諾 – Hamza

回答

1

下面是代碼和解釋。

  • 請務必記住,如果您使用自動佈局約束,請勿設置設置幀。佈局引擎將遍佈它們。如果您在代碼中實例化您的視圖,請勿設置框架,或者如有必要,如果將框架設置爲CGRect.zero,則它將最好地傳達信息。
  • 瞭解視圖生命週期。具體來說,你可以設置你的限制在viewDidLoad,他們應該只創建一次。
  • 請記住設置自動調整大小屏蔽爲false。在學習代碼中的自動佈局時,這是最常見的錯誤。
  • 實際上有三種創建約束的方法,以及幾種激活它們的方法。在你的問題中,我認爲最簡單的方法是使用錨點。

這裏的定心用的343的寬度和264的高度的視圖(任何視圖)的示例:

let myView = UIView() // note, I'm not setting any frame 

override func viewDidLoad() { 
    super.viewDidLoad() 
    view.translatesAutoresizingMaskIntoConstraints = false 
    myView.translatesAutoresizingMaskIntoConstraints = false 
    myView.widthAnchor.constraint(equalToConstant: 343.0).isActive = true 
    myView.heightAnchor.constraint(equalToConstant: 264.0).isActive = true 
    myView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true 
    myView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true 
} 

這一切就是它!但... ....

我會建議一件事。不要在設置高度和寬度時使用常量。這不是「適應性」。您的4英寸iPhone SE的屏幕尺寸爲568x320,可能看起來居中且足夠大。但在屏幕尺寸爲736x414的iPhone Plus上,它可能非常小。 (不要說12.9英寸的iPad Pro!)

請注意我的代碼如何使用superview作爲centerX/centerY錨點。 (而不是equalToConstant這是equalTo。)做同樣的寬度和高度。通過使用multiplierconstant以及UILayoutGuide,您可以使您的佈局適應Apple向您投放的任何屏幕尺寸。

+0

謝謝!!!!你能問我一個問題嗎? :) – Hamza

1

你可以試試這個

avPlayerController.view.enableAutoLayoutConstraint() 
avPlayerController.view.setCenterXConstraint(.equal, constantValue: 0) 
avPlayerController.view.setCenterYConstraint(.equal, constantValue: 0) 

extension UIView 
{ 
    //Method to making translatesAutoresizingMaskIntoConstraints false. 
    func enableAutoLayoutConstraint() 
    { 
     self.translatesAutoresizingMaskIntoConstraints = false 
    } 
    //Method to set Center-X Consttraint 
    func setCenterXConstraint(_ relationType:NSLayoutRelation , constantValue:CGFloat)->NSLayoutConstraint 
    { 
     let constraint = NSLayoutConstraint(item: self, attribute:.centerX, relatedBy: relationType, toItem: self.superview, attribute: .centerX, multiplier: 1, constant: constantValue) 
     self.superview?.addConstraint(constraint) 
     return constraint 
    } 

    //Method to set Center-Y Consttraint 
    func setCenterYConstraint(_ relationType:NSLayoutRelation , constantValue:CGFloat)->NSLayoutConstraint 
    { 
     let constraint = NSLayoutConstraint(item: self, attribute:.centerY, relatedBy: relationType, toItem: self.superview, attribute: .centerY, multiplier: 1, constant: constantValue) 
     self.superview?.addConstraint(constraint) 
     return constraint 
    } 

} 
+0

謝謝!!!!你能問我一個問題嗎? :) – Hamza

相關問題