After creating a custom UIView
, how can you use Storyboard to add AutoLayout constraints relating its subviews to views/components outside the custom UIView
?
In the example below, SliceView
represents a custom UIView
. In its .xib file there is a UILabel
.
After adding a new UIView
and setting its class to SliceView
, the new view looks the same in Storyboard. It hasn't inherited the subviews and properties of a SliceView.
The two screenshots below illustrate the view hierarchy in the SliceView.xib
and how after transforming a UIView
into a SliceView
, the hierarchy remains the same in Storyboard.
So if you want to add a button that is constrained to 10 pixels above the SliceView
label, how can you do that inside Storyboard?
class SliceView: UIView {
@IBOutlet var storyboardView: UIView!
@IBOutlet weak var captionLabel: UILabel!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
if let nibsView = NSBundle.mainBundle().loadNibNamed("SliceView", owner: self, options: nil) as? [UIView] {
let nibRoot = nibsView[0]
self.addSubview(nibRoot)
nibRoot.frame = self.bounds
}
}
}
Storyboard view hierarchy:
SliceView
view hierarchy: