I created a custom UIView class where I create 2 UILabels and 1 UIImageView. I set the constraints for each element. Since the Labels will hold dynamic text each view will have a different height. How can I resize the height of the view? the constraints (top to bottom) are:
View Top > 16 > Label1 > 8 > ImageView > 8 > Label2 > 16 > View Bottom
The problem is: I have to initialize the UIView with a frame. and no matter what I try the view always uses the height of the frame given by initializing. And I cannot calculate the height of all elements and set a new frame because the labels habe sometimes 1 and sometimes 5 lines.
This is how the view looks at runtime This is how its supposed to look like
Thanks in advance
You have to initialize the view with a frame, but when setting translatesAutoresizingMaskIntoConstraints
to false
after initializing, the frame passed in becomes irrelevant. Therefore, you can do what @BallpointBen recommended in the comments and pass in .zero
:
let newCustomView = CustomView(frame: .zero)
newCustomView.translatesAutoresizingMaskIntoConstraints = false
// auto layout constraints should now determine height
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.