Minimum size to custom views

Viewed 896

I have a @IBDesignable custom view.

That i want to have a minimum height.

If i add the view in code i override

override init(frame: CGRect) {
    let height = frame.height > minHeight ? minHeight : frame.height
    let minFrame = CGRectMake(frame.origin.x, frame.origin.y, frame.width, height)
    super.init(frame: minFrame)
}

to set minimum height.

when added in storyboard the

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

is called.

So where can i set a minimum height?

Thanks

1 Answers

You can add a minimum height constraint like that

var minimumHeightConstraint = NSLayoutConstraint(item: view,
                                                         attribute: .height,
                                                         relatedBy: .greaterThanOrEqual,
                                                         toItem: nil,
                                                         attribute: .notAnAttribute,
                                                         multiplier: 1,
                                                         constant: minHeight)

and then add the constraint to the parent of the view

Related