0

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

我这样做的方式如下:

[myTextView setInputAccessoryView:myToolbar];

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

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

4

1 回答 1

5

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

在您看来DidLoad:

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

}
于 2012-06-27T17:02:59.347 回答