BGS
BGS

Reputation: 1440

tableview does not resize after parent view resize

I have tableview and several controls (text fields, buttons and labels) beneath it.

When keyboard shows up I can either reduce parent view height or slide it up so my controls are accessible.

That part works fine.

However I want to adjust tableview also, so I tried reducing its height (if I reduce the height of the parent view) or moving down the origin.y coordinate (if I slide parent view up).

Neither worked, i.e. tableView would not change to the new frame, it stays the same. Tableview only resizes if I do not manipulate parent view, i.e. if I adjust tableView frame alone.

Here is the methods for that:

-(void)resizeTbl:(int)pnts{
CGRect tblframe = self.myTable.frame;
//tblframe.origin.y+=pnts;
tblframe.size.height-=pnts;
self.myTable.frame =tblframe;}


-(void )keyboardWillShow:(NSNotification *)notif{
int pnts=160;
CGRect frame = self.view.frame;
//frame.origin.y-=pnts;
frame.size.height-=pnts;
self.view.frame = frame;
[self resizeTbl:pnts];}

I probably could move all the controls up one by one and then table resize would work but I think there should be an easier way. Any advice would be greatly appreciated.

Update/workaround: Since table resize works alone just fine, I added one more observer - keyboardDidShow, and moved resize myTable from keyboardWilShow into there to take care of tableview after keyboard is up.

-(void)keyboardDidShow:(NSNotification *)notif{
[self resizeTbl:160];}

So all the views come up as they are supposed to now.

However, when focus moves from one textbox to another with the keyboard already up, tableView resizes to its original frame by itself and I cannot catch when and how that does it. And then, when keyboard goes down tableView expands beyond its original frame, because I must have

-(void)keyboardDidHide:(NSNotification *)notif{
[self resizeTbl:-160];}

However, when focus changes again tableView shrinks back to its normal frame and everything looks just fine. If I could somehow prevent those unwanted tableview resizes that mess things up.

If someone could makes sense out all of this I would be very appreciative.

Update:

I got it. Autolayout was messing me up. I turned it off like that and my logic now works, no glitches.

Upvotes: 0

Views: 950

Answers (2)

catcyborg
catcyborg

Reputation: 344

You can't change self.view.frame. You should have a container view above self.view that holds self.myTable and all other controls you want. self.myTable and other controls should have the correct autoresizingMask.

When keyboard is shown, you resize the container view and all other views will respect their autoresizingMasks.

-(void )keyboardWillShow:(NSNotification *)notif{
    int pnts=160;
    CGRect rect = self.view.bounds;
    rect.size.height-=pnts;
    self.containerView.frame = rect;

    // the following should not be needed if self.myTableView.autoresizingMask
    // is at least UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleTopMargin
    // but you can try adding this
    self.myTableView.frame = self.containerView.bounds;
}

Upvotes: 0

Dima
Dima

Reputation: 23634

What is the autoResizingMask of the view set to?

Try this when you initialize the table view.

self.myTable.autoresizingMask = UIViewAutoresizingFlexibleHeight;

This will cause the table view to be resized with its parent view.

Upvotes: 1

Related Questions