2017-07-10 132 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

相关问题