iOS 8键盘隐藏了我的文本视图
我有一个UIViewController
呈现使用self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;
现在在iOS 8中,最后一个UITextView
在键盘上被隐藏起来。 如何避免它?
@奥列格的解决scheme是好的,但它没有考虑到键盘尺寸的变化,而它已经显示..他也硬编码的animation持续时间和其他一些参数。 见下面我的解决scheme:
将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中删除键盘观察者。
为了解决你的问题,你应该改变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]; }]; }
上面的@ newton_guima的答案迅速版本 incase任何人都想要它。
将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)