iOS 8 keyboard hides my textview

2019-01-10 19:42发布

I have a UIViewController presented using self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet; and now in iOS 8 the last UITextView is hidden from the keyboard when it will pushed up. How to avoid It?

3条回答
不美不萌又怎样
2楼-- · 2019-01-10 19:52

@Oleg's solution is good but it doesn't take into consideration keyboard changes in size while its already showing.. also, he hardcoded the animation duration and a few other parameters. See my solution below:

Add an observer of UIKeyboardWillChangeFrameNotification to the viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

And then add the method:

- (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];
}

Don't forget to remove the keyboard observer in both viewDidUnload and in Dealloc.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-10 19:53

For solving of you're problem you should change frame or constrain of the textview.

Small tip for you:

Use Notification to detect when keyboard will show or hide.

Sample of notification:

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

And than change the one of the parameters that are mention above.

Example of changing of frame:

- (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];
                     }];
}
查看更多
祖国的老花朵
4楼-- · 2019-01-10 20:04

Swift Version of @newton_guima's answer above incase anyone wants it.

Add observer of UIKeyboardWillChangeFrameNotification to viewDidLoad():

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardFrameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)

Then add this method:

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()
}

Then remove the observer wherever you transition away from the view or in deinit:

NSNotificationCenter.defaultCenter().removeObserver(self)
查看更多
登录 后发表回答