1

I have a view for a contact. Based on the contact info the user provides, I would like to either include labels if the information is available, or not take up space if the information is not available to avoid a bunch of fields like tel: email: etc. that are blank,

The page is laid out in storyboard. Is there a way to keep label from taking up space if empty or otherwise dynamically construct page?

I was thinking of [label sizeToFit]; but this still seems to leave at least one line of empty space for the label.

Edit: Shows vertical constraint set to 10.f and 0.f. The image is shrinking but not the space it occupied.

enter image description here

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user1904273
  • 4,562
  • 11
  • 45
  • 96
  • 2
    Possible duplicate of [How to use auto-layout to move other views when a view is hidden?](http://stackoverflow.com/questions/18065938/how-to-use-auto-layout-to-move-other-views-when-a-view-is-hidden) – Robotic Cat Oct 04 '15 at 12:37

2 Answers2

1

You can use constraints for this.

Building programmatically an advanced layout is very complicated and error prone but using Auto-Layout options you can do this.

For example you have a UILabel and sometimes you want to remove it from layout. So you can make a reference to your height and width constraint of that UILabel and so change as below:

widthConstraint.constant  = 0.0f;
heightConstraint.constant = 0.0f;

Also you can keep the initial value in a property and use them when you want to add to Layout.

---Edited---

For taking effect you should call [self needsUpdateConstraints]; after change of constraint.

Mehdi Hosseinzadeh
  • 1,160
  • 11
  • 25
  • How do you set the constraints for the label? – user1904273 Oct 04 '15 at 12:07
  • In Interface Builder you can use assistant editor mode (Interface Builder with VC) and option drag selected constraint to your code. So then manipulate it. – Mehdi Hosseinzadeh Oct 04 '15 at 12:10
  • That is shrinking the visual but leaving blank space. Is there a way to eliminate the blank space too so the whole view collapses. – user1904273 Oct 04 '15 at 12:28
  • I didn't got your problem but with right constraint assigning you can do everything. Can you send an image from your problem? – Mehdi Hosseinzadeh Oct 04 '15 at 12:30
  • For example, I placed height constraint on image. When I changed it to 0 in code, the image disappeared but the place where the image was remained. Tried same thing with label. I am not very familiar with constraints. – user1904273 Oct 04 '15 at 12:32
  • Thanks, Edited answer's content! – Mehdi Hosseinzadeh Oct 04 '15 at 12:39
  • There is probably a different constraint I should be working with but placing a constraint on the visual element and then changing the height is not changing the space. – user1904273 Oct 04 '15 at 13:06
  • If your label of o: 's vertical positioning is related to image (eg. vertical spacing between them) then it should work. But if that labels position not related to picture, Then it's behaviour is correct. O: 's Y position should be related to picture. If not you change it's vertical spacing. – Mehdi Hosseinzadeh Oct 04 '15 at 13:15
  • Ok. Marked answer correct. Seems like I have to play with constraints so they are all linked up. – user1904273 Oct 04 '15 at 13:32
0

If you're targeting iOS9+, the solution to your problem is the Stack View. https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIStackView_Class_Reference/

Here you'll find a quick introduction: https://developer.apple.com/videos/play/wwdc2015-218/

It will handle all layout for you, based on what views you give or take form it.

Z.

Zoltán
  • 1,422
  • 17
  • 22