【发布时间】:2011-07-16 10:45:05
【问题描述】:
我想问一个基本的 iphone 问题。我在 iPhone 视图中有很多 TextField,当我点击输入 TextField 时,键盘会显示并隐藏其他 TextField。我想让父视图可滚动。请给我看示例代码好吗?
非常感谢
【问题讨论】:
标签: iphone view uiscrollview uiscrollviewdelegate
我想问一个基本的 iphone 问题。我在 iPhone 视图中有很多 TextField,当我点击输入 TextField 时,键盘会显示并隐藏其他 TextField。我想让父视图可滚动。请给我看示例代码好吗?
非常感谢
【问题讨论】:
标签: iphone view uiscrollview uiscrollviewdelegate
您可以收听键盘向上和向下通知。并将您的视图移动到键盘高度上方。
在ViewWillAppear 方法中:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
在ViewWillDisAppear 方法中:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
然后有上面提到的方法来调整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;
}
【讨论】:
【讨论】:
如果父视图是 UIScrollView 则尝试在文本字段委托中类似
- (BOOL) textFieldShouldReturn:(UITextField *)theTextField { if (theTextField == textFieldName) { [scroll scrollRectToVisible:CGRectMake(0, 160, 280, 440) animated:YES];//相应地选择矩形。 } 返回是; }【讨论】: