Search code examples
iosswiftuikitconstraints

Are width and height anchors required in Auto Layout?


I've a Swift UIKit Component with dynamic sizing, am I required to specify it's width and height anchors in the parent view? If yes, then how?


Solution

  • I fixed it. Basically, inside my UIView, I had a UIButton which had a UIImage, and as I specified the UIImage was an SF Symbol with size, which I calculated like this: UIFont.preferredFont(forTextStyle: .title1).pointSize, note: the .title1 can be any UIFont.TextStyle value.

    Then what I did was, I created a Utility Method inside my UIView class, which basically returned UIImage.size, and then I called this inside my constraints method in the parent view, and the set the width and height anchor to the value from this variable.

    Everything works as expected now!