1

因此,正如希望在键盘出现时将视图向上推的人们经常建议的那样,我使用 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。

4

1 回答 1

0

我有一个类似的问题,我的文本字段没有滚动到视图中。
苹果代码并不完美,我不得不修改它:尝试通过在行中添加活动字段的高度来解决这个问题:

frame.origin.y + frame.size.height - keyboardSize.height

如果这不起作用,请将 44 添加到 scrollpoint.y (导航栏的大小)

ios7更新:

我改进了 ios7 中文本字段的不需要和错误的滚动,这些文本字段已经在视图中

if (scrollPtY < 0) {
        return;
}
于 2013-02-27T01:53:46.047 回答