无法设置UIKeyboard附件视图

And*_*nov 0 uitextfield uikit uikeyboard ios ios5

我有一个UIToolbar包含UITextField内部的实例.我想将工具栏设置为UITextField它包含的附件视图.

我这样做的方式如下:

[myTextView setInputAccessoryView:myToolbar];
Run Code Online (Sandbox Code Playgroud)

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

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

jjv*_*360 5

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

在你的viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

在视图控制器的代码中的某处:

-(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);
    }];

}
Run Code Online (Sandbox Code Playgroud)