Make a fixed size UIView, like UISwitch (using IBDesignable)
Asked Answered
S

1

13

When I'm using this guide to create a view that is designed in a XIB, reusable from within a storyboard using the IBDesignable attribute on my UIView subclass, how do I make it have a fixed size, and have its resizing behavior match that of a view like UISwitch?

With "resizing behavior" I also mean while designing in interface builder.

Subtraction answered 18/2, 2015 at 20:25 Comment(0)
L
12

You could override intrinsicContentSize() in your UIView subclass. Then you won't need to supply height and width constraints in the interface builder.

override var intrinsicContentSize: CGSize {
    return CGSizeMake(width: 100, height: 100)
}

If you only want to supply one of the dimensions, you can use UIView.noIntrinsicMetric instead of a value.

override var intrinsicContentSize: CGSize {
    return CGSizeMake(width: UIView.noIntrinsicMetric, height: 100)
}
Laicize answered 28/11, 2015 at 17:14 Comment(2)
Just an aside: IB doesn't seem to render the size correctly unless you put position constraints on the view. This tripped me up at first.Wit
FYI: In Swift 3, Xcode shows the signature as override var intrinsicContentSize: CGSize { }Fixed

© 2022 - 2024 — McMap. All rights reserved.