0

I'm just starting with iOS development and I was trying to achieve something that doesn't seem to work so far for me...

Here's the situation: I have a nib file in which I have placed a UITableView, and just underneath a UIToolbar. That works just fine, the scaling is fine if I try different screensizes etc... So I was happy.

But here's the problem: If the toolbar should be visible or not is a choice that a user can make somewhere in the application. However when the users selects to not see the toolbar I just call the method setHidden on the toolbar and pass it 'YES'. The toolbar is now gone when I try this but the UITableView is not strechted to the bottom of the screen which gives me quite an ugly result.

So here's finally the question: How can I automatically let the view stretch to the bottom when hiding toolbar? I guess I will have to do it in code (and not just some configuration option somewhere) but as I'm coming from Android this is somewhat strange for me.

dirkvranckaert
  • 1,364
  • 1
  • 17
  • 30
  • Are you using Auto-Layout (for iPhone 5 support)? – gtmtg Nov 20 '12 at 11:35
  • yes I am using the auto-layout feature – dirkvranckaert Nov 20 '12 at 11:37
  • "If the toolbar should be visible or not is a choice that a user can make somewhere in the application." Are you sure user need this choice? To me, this translate to: "I couldn't decide wether or not to show the toolbar, so I added one more option to the preferences, and will let the user cope with that choice". You could try to find a way to let the user interact with the toolbar to hide / show it. Or just make a choice. – Guillaume Nov 20 '12 at 11:44
  • In the logic of the application the user really needs to make this choice here :-) I think dantastic his answer is the most accurate, although I am a little disappointed how these things work in iOS compared to Android... But it's a small price to pay.. :-) – dirkvranckaert Nov 20 '12 at 11:57

2 Answers2

1

Your best option will probably be to resize the tableview frame as you show/hide the tool bar.

CGRect frame = myTableView.frame;
frame.size.height += toolbar.frame.height;
myTableView.frame = frame;
dirkvranckaert
  • 1,364
  • 1
  • 17
  • 30
dantastic
  • 134
  • 1
  • 10
  • can I always be sure about the 44 pixels that I have to add? I mean with other devices the rendering of the toolbar will always be the same height? – dirkvranckaert Nov 20 '12 at 11:44
  • No. you should really modify the height of the first view using the height of the view you are moving. That way it will be true for any view. (All your screen objects are descendants of UIView so they will all have a frame property you can use.) – dantastic Nov 20 '12 at 11:49
  • Allright thanks for the solution. I edited your answer to contain the toolbar height instead of the +44 – dirkvranckaert Nov 20 '12 at 11:55
  • I'd actually disagree with this answer. @dirkvranckaert is using Auto-Layout, so the "proper" way to do this would be to create a height constraint for the table view and modify its constant. – gtmtg Nov 20 '12 at 12:01
  • And how can I change the constraints? – dirkvranckaert Nov 20 '12 at 12:57
  • @dirkvranckaert See my answer below. – gtmtg Nov 20 '12 at 17:09
-1

Because you're using Auto-Layout, you'd want to create a height constraint for the UITableView and link it with your view controller via an IBOutlet. Then, to modify the height of the UITableView, simply do:

_tableViewHeightConstraint.constant += toolbar.frame.height;

You can even animate this with:

[UIView animateWithDuration:0.25 animations:^{
    _tableViewHeightConstraint.constant += toolbar.frame.height;
}];

Note that you might need to call [_tableView layoutIfNeeded] after changing the height constraint.

gtmtg
  • 3,010
  • 2
  • 22
  • 35
  • you have to call layoutIfNeeded, inside the animation block, on the parent view (because some of the constraints are added to the parent view), you don't have to change the constraint constant in the animation block, and it is preferred to call layoutIfNeeded just before the animation, so any not-updated constraints (other then the one you animate) will be laid-out. see http://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes – surui Nov 25 '14 at 10:38