2017-07-10 127 views
5

我想加載動態筆尖作爲容器的子視圖。除了子視圖有一個偏移量,我似乎無法消除掉(參見下面的圖片中的粉色視圖),我幾乎可以開始工作。使子視圖適合內容器,並正確調整大小

enter image description here

從視圖層次調試:

enter image description here

正如你可以在第二個圖片中看到,容器框架正確定位,而子視圖不,出於某種原因

我真的不知道自動佈局會怎麼樣。

這裏是與裝載筆尖和子視圖分配給它涉及代碼:

enter image description here

被註釋掉的代碼是我一直在努力,使其工作的事情,但沒有成功。我認爲自動佈局可以獨立工作,無需我做任何事情,但默認情況下,它會加載筆尖而不調整其大小。

這意味着領先和頂級錨是正確的,但是筆尖然後使用它的全尺寸...(以下CF圖片)

enter image description here

所以現在的問題是,什麼是需要我爲了加載筆尖並使其適合容器視圖嗎?

回答

4

您應該爲您的NibView添加約束,而不是設置NibView的邊界和框架。

嘗試呼籲NibView下面的函數(addFullScreenConstraint)將NibView作爲內容視圖的子視圖後:

extension UIView { 

    /// Adds constraints to this `UIView` instances `superview` object 
    /// to make sure this always has the same size as the superview. 
    /// Please note that this has no effect if its `superview` is `nil` 
    /// – add this `UIView` instance as a subview before calling this. 
    func addFullScreenConstraints() { 
     guard let superview = self.superview else { 
      return 
     } 

     self.translatesAutoresizingMaskIntoConstraints = false 
     superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[subview]-0-|", 
                   options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) 
     superview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[subview]-0-|", 
                   options: .directionLeadingToTrailing, metrics: nil, views: ["subview": self])) 
    } 
} 
+0

聖鱷梨,它終於成功了!非常感謝 – Skwiggs

相關問題