0

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:

After adding SliceView

SliceView view hierarchy:

enter image description here

Crashalot
  • 33,605
  • 61
  • 269
  • 439
  • Actually i dont understand what u want ? – yankit Patel Jan 19 '16 at 08:26
  • The subviews of the custom view is not appearing in the Scene Inspector so there's no way to set constraints against them. How do you use AutoLayout against the subviews of a custom view? – Crashalot Jan 19 '16 at 08:37
  • @yankitPatel do you happen to know the answer to this? http://stackoverflow.com/questions/34912050/avoiding-blurriness-at-start-end-of-video-even-after-using-setpreferredvideos?noredirect=1#comment57575685_34912050 – Crashalot Jan 21 '16 at 10:59
  • No i am not... sorry – yankit Patel Jan 22 '16 at 05:13

0 Answers0