简体   繁体   中英

Auto layout: Pin top of view to bottom of another view programatically

I am switching my XIB file over to auto layout and have run into this problem I can't seem to find a good answer for on stack.

I'm looking to pin the top of one view to the bottom of another view programatically. Essentially, the top view pushes the bottom view downwards causing the height of the bottom view to be reduced proportionally while staying anchored at the bottom.

The top view is a fixed height while the bottom view is variable. In pseudo code, I essentially want this:

If the top view is present, pin the top portion of the bottom view, to the bottom of the top view while being anchored at the bottom (or reduce height to compensate for the top view)

Otherwise, the bottom view will be displayed at full height, essentially height = top view + reduced height bottom view 

This needs to be done programmatically because the top view is only displayed conditionally, otherwise I want the bottom view to take up the height that would have been introduced by the top view.

Hope this makes sense!

If you had properties topView and bottomView of your self view, you could do this:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_topView]-[_bottomView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_topView, _bottomView)]];

Setting your top view's height to zero would answer your hiding question, too.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM