2

I have a custom control (xib + UIView subclass).

I'd like my control to have a computed height, like UITextField or ProgressBar, so I'd like IB not to show me the UIView resize knobs on each side, but only on the left and right of my custom control. In UITextField height field in property inspector is ghosted, so you can't modify it from Interface Builder.

I searched StackOverflow, and found this Make a fixed size UIView, like UISwitch (using IBDesignable), but it isn't what I am trying to achieve.

Thank you for your help, Luca-

Graham
  • 7,431
  • 18
  • 59
  • 84
Luca
  • 303
  • 2
  • 13
  • 2
    AFAIK, Apple doesn't expose methods to disable sizing handles/values of designable views in IB. (And even controls like `UISwitch` allow IB to add constraints to change the size, so it's not quite as "fixed" as it appears.) By the way, in addition to setting `intrinsicContentSize` as suggested in that other question, you might want to set `contentHuggingPriority` and `contentCompressionResistancePriority` to 1000, too. But all that does is make sure that IB will use your size when it applies auto-layout if you don't define other constraints to override this. – Rob May 06 '17 at 20:47

2 Answers2

2

I believe that you want to disable height in Size inspector in right utilities panel, just for your custom control like segment control and fixed height text field. As of now, this is not possible. Using IBInspectable, you will only be able to display your variables in Attribute Inspector. The other option you have is to resize your control within the code, every time it loads.

Shaheen M Basheer
  • 1,010
  • 6
  • 11
  • Ok, thank you for your reply. Is there a way to reset my custom control size to computed width/height every time it gets changed from Interface Builder? – Luca May 06 '17 at 21:02
1

One way to accomplish what you want is to add height and width constraints on the view using Auto Layout. You can add an IBOutlet to the constraints in Interface Builder in case you need to manipulate them at run time.

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • Do you mean constraints on the main view of my custom controller? I could be wrong, but it seems to me I can't add any constraints via Interface Builder (pin / size constraints are ghosted). I added a sub UIView and added constraints to it (height and top / bottom / leading / trailing constraints), but Interface Builder presents all the knobs to resize my controller. Anyway, if I set my custom controller the wrong height (height != constraints) Xcode complains telling me that there are conflicts in AutoLayout constraints. – Luca May 06 '17 at 20:15
  • ViewController's don't have sizes (heights and widths). Only views. A ViewController, generally speaking, displays a full screen of information and will take up the entire screen. The exception is when a view controller is part of a view controller hierarchy where the parent structures the children... that includes things like UINavigationController, UISplitViewController, or UIPageController. As a result you generally are not responsible for setting the size of the root view of a view controller. I guess I don't understand what effect you are trying to achieve. – Scott Thompson May 07 '17 at 00:27
  • Sorry, I wrote "custom controller" while I meant "Custom Control", a UIView subclass with some UIKit elements (basically a UITextField and 2 labels). – Luca May 07 '17 at 09:02