因此,正如通常建议那些希望在键盘出现时推高视图的人,我使用的是UIScrollView。我复制并粘贴了此页面中的信息 - http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7 - 来做那个......
但是,当我点击文本字段时,键盘弹出,但视图实际上向下滚动,在顶部显示黑色。在那之后,我实际上可以手动滚动视图并显示隐藏的文本字段......显然,这不是我想要做的!
有什么想法吗?我在viewDidLoad()方法中有以下内容: - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
}
我已经尝试过这个版本的keyboardWasShown:
- (void) keyboardWasShown:(NSNotification *)aNotification
{
NSDictionary * info = [aNotification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0,keyboardSize.height, 0);
[[self introScroll] setContentInset:contentInsets];
[[self introScroll] setScrollIndicatorInsets:contentInsets];
CGRect ltdRect=[[self view] frame];
ltdRect.size.height=keyboardSize.height;
if (!CGRectContainsPoint(ltdRect,[self activeField].frame.origin))
{
CGPoint scrollPoint = CGPointMake(0,[self activeField].frame.origin.y-keyboardSize.height);
[[self introScroll] setContentOffset:scrollPoint animated:YES];
}
NSLog(@"Keyboard out!");
}
以及此版本的keyboardWasShown:
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = [[[self activeField] superview ]frame];
bkgndRect.size.height += kbSize.height;
[[[self activeField] superview] setFrame:bkgndRect];
[[self introScroll] setContentOffset:CGPointMake(0.0, [self activeField].frame.origin.y-kbSize.height) animated:YES];
}
两者都给我完全相同的结果。
发生了什么事?我正在使用Xcode 4.6。
答案 0 :(得分:0)
我遇到类似的问题,我的文字字段没有滚动到视图中 苹果代码不完美,我不得不修改它: 尝试通过在行中添加活动字段的高度来解决这个问题:
frame.origin.y + frame.size.height - keyboardSize.height
如果这不起作用,请向scrollpoint.y(导航栏的大小)添加44
ios7更新:
我在ios7中为已经在视图中的文本字段改进了不需要的和错误的滚动 由
if (scrollPtY < 0) {
return;
}