2012-06-27 31 views
0

我有一个UIToolbar的实例,里面包含UITextField。我想为它包含的UITextField在附件视图中设置工具栏。故障设置UIKeyboard附件视图

我做到这一点的方法如下:

[myTextView setInputAccessoryView:myToolbar]; 

当我编译和运行代码,当我按在文本字段整个键盘消失。我特别确定我正在设置inputAccessoryView而不是inputView。似乎整个输入视图都被取代了,没有明确的指示。

有没有人知道一种方法来解决这个问题?

回答

5

它通常并不好把一个文本字段中输入附件视图......如果你把工具栏沿视图的底部,然后用UIKeyboardWillChangeFrameNotification使用键盘移动工具栏什么是更好的是.. 。

在您的viewDidLoad:

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

} 
+0

这似乎像什么内置的消息应用程序与做文本输入工具栏。 – Dima

+0

是的,我认为是的,它也是一个简单的移动全屏滚动视图,文本视图或Web视图的方法,这样即使键盘覆盖它也可以滚动到结尾... – jjv360

+0

在这种情况下,是endFrame在那里确保我将工具栏向正确的方向移动? (当键盘在消失时上下移动时)? –