【发布时间】:2014-08-16 06:42:40
【问题描述】:
在底部的 ViewController 中,我有一个 UIToolBar (myToolbar),其中我有一个 UITextField (messageTxt) 和一个按钮。当我尝试在 messageTxt 上写任何东西时,键盘会出现,但它会隐藏工具栏。因此,在我不关闭键盘之前,我不知道我输入了什么。 我正在寻找,当我开始打字时,我应该能够看到我正在输入的内容。为此,也许可以缩小视图并在下面添加键盘,或者如何实现。
对于其他网站,我实现了以下内容,但看不到我正在输入的内容。我可以在输入时看到建议,但看不到我正在输入的内容。你能帮我知道我哪里出错了。我的代码是:-
在我的 viewDidLoad 方法中,为键盘添加了这两个通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[super viewDidLoad];
通知方法的实现。
-(void) keyboardWillShow: (NSNotification *) notification {
UIViewAnimationCurve animationCurve = [[[notification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
NSTimeInterval animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardBounds = [(NSValue *) [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationDuration:animationDuration];
[self.myToolBar setFrame:CGRectMake(30.0f, self.view.frame.size.height - keyboardBounds.size.height - self.myToolBar.frame.size.height, self.myToolBar.frame.size.width, self.myToolBar.frame.size.height)];
[UIView commitAnimations];
}
-(void) keyboardWillHide: (NSNotification *) notification {
UIViewAnimationCurve animationCurve = [[[notification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
NSTimeInterval animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardBounds = [(NSValue *) [[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationDuration:animationDuration];
[self.myToolBar setFrame:CGRectMake(0.0f, self.view.frame.size.height - 46.0f, self.myToolBar.frame.size.width, self.myToolBar.frame.size.height)];
[UIView commitAnimations];
}
最后添加了这个 TextDelegate 方法,以便知道它何时有焦点。
-(void) textFieldDidBeginEditing:(UITextField *)textField {
//[self.messageTxt setInputAccessoryView:self.myToolBar];
}
我在哪里出错或我的代码中缺少什么? 还有一点,我希望实现 TextView inlace of TextField - 因为我想要多行输入。我相信这是可能的,并且通过 TEXtView 的上述实现也不会有任何问题。是在 View 中添加 TextView 和 Button 而不是 ToolBar 更好的选择,还是可以使用 ToolBar 很好地管理?有什么更好的选择?
非常感谢任何帮助。谢谢。
【问题讨论】:
标签: ios keyboard uitextfield uitextview uitoolbar