Interface Builder and subclassed UIView

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?

Answered by Claude31 in 687826022

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

The cause is that the size of view is not changed until you change it in size Inspector:

In fact you change the layer; not the view frame in your code.

Note: a class name should start with Uppercase: MyView

I thought it was something like that, but it means that if I don't correct the size in size inspector, I will always have the problem. IB can deserialise values by reading XIB file, but there is no way to change these values by program and serialise them back.

The case I presented was very simple, but what if I put constraints by program? In this case I don't know the exact size of MyView and if I have multiple subclassed controls, it becomes very confuse in the view of Interface Builder.

Accepted Answer

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
Interface Builder and subclassed UIView
 
 
Q