我尝试使用UISearchBar作为输入附件视图,以便它可以与键盘一起显示动画,我的第一个解决方案是,
这适用于我的带有iOS 4.3的iPhone 4。但是,在iPhone 5.0模拟器上,只有点击搜索栏后键盘才会显示(参见下图)。
然后我尝试我的第二个解决方案,
使用此解决方案,我可以轻松显示键盘,并专注于输入附件视图(搜索栏),但是,当搜索栏被聚焦时,我无法输入任何文本。
任何人都知道出了什么问题? 谢谢!
答案 0 :(得分:0)
我将UISearchBar设置为自己的输入附件视图时遇到了类似的问题。首先单击 - 栏将移动,第二次单击 - 键盘显示。取下键盘完全取消了屏幕上的栏。我的解决方案是手动响应键盘显示和隐藏通知,并取消输入附件。然后我会相应地为酒吧制作动画。如果您的搜索栏不在底部,就像我的搜索栏一样,您必须根据需要定制动画。我希望这是有帮助的!
在viewDidLoad中:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
其他方法:
- (NSTimeInterval)keyboardAnimationDurationForNotification:(NSNotification*)notification {
NSDictionary* info = [notification userInfo];
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 0;
[value getValue:&duration];
return duration;
}
-(void)keyboardWillShow:(NSNotification*)n {
CGRect frame = CGRectMake(self.mSearchBar.frame.origin.x, self.mSearchBar.frame.origin.y - 216, self.mSearchBar.frame.size.width, self.mSearchBar.frame.size.height);
[UIView animateWithDuration:[self keyboardAnimationDurationForNotification:n] delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
self.mSearchBar.frame = frame;
} completion:nil];
}
-(void)keyboardWillHide:(NSNotification*)n {
CGRect frame = CGRectMake(self.mSearchBar.frame.origin.x, self.mSearchBar.frame.origin.y + 216, self.mSearchBar.frame.size.width, self.mSearchBar.frame.size.height);
[UIView animateWithDuration:[self keyboardAnimationDurationForNotification:n] delay:0 options:UIViewAnimationOptionLayoutSubviews animations:^{
self.mSearchBar.frame = frame;
} completion:nil];
}
完成后删除观察者是一个好主意(可能在viewDidUnload中):
[[NSNotificationCenter defaultCenter] removeObserver:self];