Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView Auto-Layout Flexible Height

I have following design. enter image description here

Am using AutoLayout to make every thing flexible. In this design i have a UIView which is Gray in Color as showing in image and a UITableView below UIView. Some times i have to show this UIView and some times i have to hide this Gray UIView.

My problem is when ever i hide UIView, UITableView is not fixing its height. I don't want to hard code in .m file. Is it possible AutoLayout take care of this issue. I have these constraints as below image. Am i missing any constrain.

enter image description here

When i try to change UIView height, UITableView is not moving up and showing some orange constraints as show in image. enter image description here

like image 802
user2706827 Avatar asked Mar 15 '26 09:03

user2706827


1 Answers

The contraints looks good. All you have to do to show/hide the gray UIView is change the height constraint constant. To do this, create an IBOutlet in your controller for the constraint (you do this the same way you would for a UIView IBOutlet), and when you want to hide the gray view, set the constant property of the constraint to 0.

eg.

@interface MyViewController
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *greyViewHeightConstraint;
@end

and when you want to hide the view:

self.greyViewHeightConstraint.constant = 0;

To show the view again, you would have to save the "default" constant value after the storyboard is loaded (like in viewDidLoad for example), and set self.greyViewHeightConstraint.constant to this saved value.

Note also that these constraint changes can be animated.


The "orange constraint" you are seeing in Interface Builder is normal: it indicates that the view frame is not matching the constraints you set. You can then update the frame to respect the constraints, or update the constraints to match the frame.

like image 77
Guillaume Algis Avatar answered Mar 17 '26 22:03

Guillaume Algis