ThisClark ThisClark - 1 year ago 115
iOS Question

Get the intrinsic height of a custom control

How can I get the height of my custom control?

The idea is I will use it to dynamically set the height of some buttons inside the custom control. I've set the Placeholder height to 44 in the Xcode size inspector.

enter image description here

Working off Apple's Start Developing iOS Apps (Swift) tutorial, I am attempting to access

and it gives a value of 1000 while the tutorial seems to suggest it should be 44.

class RatingControl: UIView {
override public var intrinsicContentSize: CGSize {
let buttonSize = Int(frame.size.height)
print(buttonSize) // prints 1000
let width = (buttonSize * starCount) + (spacing * (starCount - 1))
return CGSize(width: width, height: buttonSize)

Answer Source

You should never access frame inside intrinsicContentSize. intrinsicContentSize should return the size that perfectly fits the contents of the view, regardless of its current frame.

In your case, I think you can just use 44 for your buttonSize.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download