问题
I would like to ask a basic iphone question. I have many TextFields in iPhone view, when I tap to input in a TextField, the Keyboard shows up and hide other TextField. I would like to make the parent view scrollable. Would you please show me the example code?
Thank you very much
回答1:
You can listen for the keyboard up and down notification. and move your view just above height of keyboard.
In the ViewWillAppear
method:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
In the ViewWillDisAppear
method:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
And then have methods referred to above to adjust the position of the bar:
-(void) keyboardWillShow:(NSNotification *) note
{
CGRect r = bar.frame, t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
r.origin.y -= t.size.height;
bar.frame = r;
}
-(void) keyboardWillHide:(NSNotification *) note
{
CGRect r = bar.frame, t;
[[note.userInfo valueForKey:UIKeyboardBoundsUserInfoKey] getValue: &t];
r.origin.y += t.size.height;
bar.frame = r;
}
回答2:
This will give you an idea
How to make a UITextField move up when keyboard is present?
回答3:
If the parentview is UIScrollView then try something like in textfield delegate
- (BOOL) textFieldShouldReturn:(UITextField *)theTextField { if (theTextField == textFieldName) { [scroll scrollRectToVisible:CGRectMake(0, 160, 280, 440) animated:YES];//choose the rect accordingly. } return YES; }
来源:https://stackoverflow.com/questions/5323274/iphone-scroll-in-a-view