Nimrod Gutman
Nimrod Gutman

Reputation: 1087

iOS resize view with auto layout enabled

The given problem

I have the following view that includes a Navigation Bar, Search Bar and table view - the left picture is the given view I have and the right one is what I want to achieve - basically just hide the navigation bar and resize everything.

given wanted

The current solution

Currently I've managed to hide and resize everything using a vertical-space constrain from the search box to the superview top property -

vertical-constraint

What I want

I'm looking for the easiest way to hide the navigation bar and resize the search box + table to fill the whole screen.

Is there any way to do it and take advantage of the iOS 6 auto-layout system?

My current solution feels unnatural.

Upvotes: 6

Views: 5312

Answers (2)

larsacus
larsacus

Reputation: 7346

If that's the only constraint you need to change, then you can create an IBOutlet for that one constraint into your view or view controller, then simply modify the constraint based on when you need it to change:

if(shouldHide){
  self.nibTitleBarConstraint.constant = 0.f;
}
else{
  self.nibTitleBarConstraint.constant = 44.f;
}

If you'd like to animate the change, then simply stick -layoutIfNeeded in an animation context:

[UIView animateWithDuration:0.33f
                 animations:^{
                   [view layoutIfNeeded];
                 }];

This is applicable to any constraint's constant you would like to modify (height, top space, etc.) so long as your other constraints know how to react with a change in that view's constraints.

Upvotes: 6

Rui Peres
Rui Peres

Reputation: 25907

Can you just:

[[self navigationController] setNavigationBarHidden:YES animated:NO];

Upvotes: 1

Related Questions