user798719
user798719

Reputation: 9869

iphone: How to resize a view and all of its children (in context of keyboard popping up)?

I have an iphone app. The view is consists of two children: a tableview 400 pixels tall and a uitextview at the bottom of the view that is about 80 pixels tall.

I am trying to move the textview and tableview upwards when the keyboard is visible. I resized the "main" view to make it smaller. Why don't its children appear to resize as well? Do I need to readjust the frame of the tableview, and readjust the frame of the textview as well? I'm just wondering what the expected behavior is. Do I need to use setNeedsLayout() or setNeedsDisplay?

I expected that by downsizing the "main" view, its children would be "shrunk" and constrained by their parent view as well. It looks like the textview remains at the bottom of the screen. The tableview appears to be correct (it shrunk), but the textview doesn't look like it moves. Although in the new shrunken view there is now a white area where the textview should move to.

// Called when the UIKeyboardDidShowNotification is sent.

- (void)keyboardWasShown:(NSNotification*)aNotification {

NSLog(@"keyboard was shown");
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//CGRect keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
NSLog(@"kb size is:%f",kbSize.height);

//UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);

CGRect newFrame = self.view.frame;
//20 status bar height, 44 is nav bar height
newFrame.size.height = 480 -20 - 44 - kbSize.height;

self.view.frame = newFrame;
self.view.layer.borderWidth = 1.0f;
self.view.layer.borderColor = [[UIColor redColor] CGColor]; 

[self.view setNeedsLayout];
[self.view setNeedsDisplay];

}

Upvotes: 1

Views: 185

Answers (2)

Hanon
Hanon

Reputation: 3927

Use UIScrollView for your contents.

When notification received, expand the content size of the scrollview with keyboard height. And then set the content offset.

Upvotes: 0

Roberson Balbino
Roberson Balbino

Reputation: 397

Try use UIScrollView. Put this controls inside scrollview and use property contentOffset to scroll up/down. This is very easy way.

Good luck!

Upvotes: 2

Related Questions