Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

givenwanted

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 -

  • The default constraint constant is 44 (the navigation bar height)
  • After clicking the search box I hide the navigation bar and set the constraint constant to be 0
  • When I stop searching I restore the navigation bar and set the constraint to 44 again

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.

like image 830
Nimrod Gutman Avatar asked Oct 29 '12 15:10

Nimrod Gutman


2 Answers

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.

like image 80
larsacus Avatar answered Nov 13 '22 06:11

larsacus


Can you just:

[[self navigationController] setNavigationBarHidden:YES animated:NO];
like image 1
Rui Peres Avatar answered Nov 13 '22 06:11

Rui Peres