iOS 8键盘隐藏了我的textview

时间:2014-10-06 09:46:25

标签: ios cocoa-touch uikeyboard uimodalpresentationstyle

我使用UIViewController显示了self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;,现在在iOS 8中,当它向上推时,键盘上隐藏了最后一个UITextView。如何避免呢?

3 个答案:

答案 0 :(得分: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中删除键盘观察者。

答案 1 :(得分:5)

快速版@newton_guima's以上答案,任何人都想要它。

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)

答案 2 :(得分:3)

要解决您的问题,您应该更改frame的{​​{1}}或constrain

小提示:

使用通知检测键盘何时显示或隐藏。

通知示例:

textview

而不是改变上面提到的参数之一。

更改框架的示例:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
相关问题