Gilian Joosen
Gilian Joosen

Reputation: 486

iOS Auto layout view heigh based on other view

I am developing an app right now, but I have the following problem. I have 2 views (top view will be an imageview and the bottom view will be a UIWebView), to simplify the concept I have made a storyboard with 2 colored views.The bottom view should fit to the left, right and bottom. The top view should fit to the left, right and top. But the height of the content of the top view can change, so I've added the constraint: topView.bottom == bottomView.top, but now Xcode complains and says: "Need constraints for:Y position or height" but the y position is based on the height, and the height has a constrain to the top, how could I fix this? (I can't use the StackView, because the minimum iOS version has to be iOS 7.0)

Thank you very much already, the picture is down below.

enter image description here

Upvotes: 0

Views: 61

Answers (1)

Kubba
Kubba

Reputation: 3438

Xcode complains because layout is unambiguous. Add height constraint to topView and make an outlet to it:

@IBOutlet weak var topViewHeightConstraint: NSLayoutConstraint!

Than load your image into your imageView and get its height. After that adjust the height of your view by:

self.topViewHeightConstraint.constant = heightOfTheImage
self.topView.layoutIfNeeded()

Upvotes: 1

Related Questions