I test a very simple thing:
I have a Subclassed UIView as follow:
@IBDesignable
class myView: UIView {
override public init(frame: CGRect) {
super.init(frame: frame)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override open func layoutSubviews() {
super.layoutSubviews()
layer.frame.size.height = 50
layer.borderWidth = 1
layer.borderColor = UIColor.green.cgColor
}
}
To install this component in Interface Builder, I add an UIView and I change the class name in the Inspector.
When I add the UIView, the height of this view is 128. I suppose it is a default value of IB. But I want this height to be 50.
When I change the name of the class in the IB inspector, the view is correctly drawn, white a height of 50, but when I select it, the size of the selection is the size of a default UIView
Is there a mean to correct this?
AFAIU, what happens is that The UIView is created from the size defined (128 here). Then Xcode runs IBDesignable that adjusts the size for display.
But underlying size remains the same unless you change it.
What is really the problem it causes, beyond the mere annoyance when you manipulate object ? I experienced the same when resizing Switch button and did make with it without much concern.
So, I see a few options (did not test all):
- Adapt the size manually in Size inspector (what you don't like I understand)
- Set a height constraint on MyView (create constraint in code)
- File a request for enhancement