我有一个UIViewController
使用self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;
,现在在 iOS 8 中,UITextView
当它向上推时,最后一个从键盘隐藏。如何避免它?
问问题
9834 次
3 回答
57
@Oleg 的解决方案很好,但它没有考虑键盘尺寸的变化,而它已经显示了......此外,他对动画持续时间和一些其他参数进行了硬编码。请参阅下面的解决方案:
添加一个观察者UIKeyboardWillChangeFrameNotification
到viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
然后添加方法:
- (void)keyboardFrameWillChange:(NSNotification *)notification
{
CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.view.frame;
CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y);
self.view.frame = newFrame;
[UIView commitAnimations];
}
不要忘记在 viewDidUnload 和 Dealloc 中删除键盘观察者。
于 2014-10-07T00:16:26.593 回答
5
@newton_guima 上面的答案的Swift 版本,以防有人想要它。
将观察者添加UIKeyboardWillChangeFrameNotification
到viewDidLoad()
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardFrameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
然后添加这个方法:
func keyboardFrameWillChange(notification: NSNotification) {
let keyboardBeginFrame = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue
let keyboardEndFrame = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue
let animationCurve = UIViewAnimationCurve(rawValue: (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardAnimationCurveUserInfoKey)!.integerValue)
let animationDuration: NSTimeInterval = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardAnimationDurationUserInfoKey)!.doubleValue
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(animationDuration)
UIView.setAnimationCurve(animationCurve!)
var newFrame = self.view.frame
let keyboardFrameEnd = self.view.convertRect(keyboardEndFrame, toView: nil)
let keyboardFrameBegin = self.view.convertRect(keyboardBeginFrame, toView: nil)
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y)
self.view.frame = newFrame;
UIView.commitAnimations()
}
然后移除观察者,无论您从视图过渡到何处或在deinit
:
NSNotificationCenter.defaultCenter().removeObserver(self)
于 2015-09-18T17:15:59.057 回答
3
为了解决你的问题,你应该改变frame
或constrain
.textview
给你的小提示:
使用通知来检测键盘何时显示或隐藏。
通知样本:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
而不是更改上面提到的参数之一。
改变框架的例子:
- (void)keyboardWillHide:(NSNotification *)notification
{
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.2
animations:^{
[self setStartingFrame:self.commentsView.frame.size.height - commentViewOutlet_.frame.size.height - tabBarOffset_];
[self.view layoutIfNeeded];
}];
}
于 2014-10-06T09:56:28.510 回答