Reputation: 1922
In my app, I have a detail UIViewController
that shows info about a restaurant. I'm trying to understand how to dynamically set the UI depending on what info is provided. The information that I'll list about the restaurant are its image, name, address, and description. The image and name of the restaurant will always be there, but its address and description are not guaranteed to be there.
This leaves me with 4 possible combinations of how the view will look:
How do I set this dynamic UI in my UIViewController
? I'm used to setting views hidden based on conditions. However, in the example that the restaurant's location is the only piece of info not available, then that gap between the restaurant's title and the restaurant's description will still be there if I understand correctly.
Is there a common practice that should be used when dealing with dynamic UI like this? Any help or advice would be appreciated. Thanks.
Upvotes: 1
Views: 42
Reputation: 12334
Make an outlet for the height constraint of the location text and the height constraint of the description text.
@property (nonatmoic, strong) @IBOutlet NSLayoutConstraint *heightForLocation;
@property (nonatomic, strong) @IBOutlet NSLayoutConstraint *heightForDescription;
If either of these pieces of information is missing and you want to "hide" the views, you will hide the views with either self.heightForLocation.constant = 0;
or self.heightForDescription.constant = 0;
. With the height of the location text view set to 0, the view will appear hidden, and the description text view will move up to make it look like there is no gap.
You can play with multiple configurations of the how the views are laid out and what vertical constraints they have, but the magic here is that "hiding" a view is really setting its height to 0. With proper vertical constraints, the views will layout accordingly.
Upvotes: 1