iOS 8键盘隐藏了我的textview

Ale*_*ani 23 cocoa-touch uikeyboard ios uimodalpresentationstyle

我有一个UIViewController使用self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;,现在在iOS 8中,UITextView当它被推高时,最后一个是从键盘隐藏的.怎么避免呢?

new*_*ima 57

@ Oleg的解决方案是好的,但它没有考虑到键盘大小的变化,而它已经显示..也,他硬编码动画持续时间和一些其他参数.请参阅下面的解决方案

观察员的加入UIKeyboardWillChangeFrameNotificationviewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
Run Code Online (Sandbox Code Playgroud)

然后添加方法:

- (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];
}
Run Code Online (Sandbox Code Playgroud)

不要忘记在viewDidUnload和Dealloc中删除键盘观察者.


myl*_*les 5

@newton_guima的答案的Swift版本以防万一有人想要它。

将的观察者添加UIKeyboardWillChangeFrameNotificationviewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardFrameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

然后添加此方法:

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()
}
Run Code Online (Sandbox Code Playgroud)

然后取出无论你从视图或转型远离观察者deinit

NSNotificationCenter.defaultCenter().removeObserver(self)
Run Code Online (Sandbox Code Playgroud)