设置 UIKeyboard 附件视图时出现问题



我有一个包含UITextFieldUIToolbar实例。我想将工具栏设置为它所包含UITextField的附件视图。

我这样做的方式如下:

[myTextView setInputAccessoryView:myToolbar];

当我编译并运行代码时,当我按下文本字段时,整个键盘消失了。我特别确保我设置的是inputAccessoryView而不是inputView。似乎整个输入视图刚刚被替换,没有任何明确的指示。

有谁知道解决这个问题的方法?

文本字段放在输入附件视图中通常不好...更好的是,如果您将工具栏放在视图的底部,然后使用UIKeyboardWillChangeFrameNotification用键盘移动工具栏......

在您看来,确实加载:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

在视图控制器代码的某个地方:

-(void) keyboardWillChange:(NSNotification*)notify {
    CGRect endFrame;
    float duration = [[[notify userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    [[[notify userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&endFrame];
    endFrame = [self.view convertRect:endFrame fromView:nil];
    float y = (endFrame.origin.y > self.view.bounds.size.height ? self.view.bounds.size.height-44 : endFrame.origin.y-44);
    [UIView animateWithDuration:duration animations:^{
        toolbar.frame = CGRectMake(0, y, self.view.bounds.size.width, 44);
    }];
}

最新更新