ios – iPad上的UIModalPresentationFormSheet.键盘出现时如何调整UITextView高度

前端之家收集整理的这篇文章主要介绍了ios – iPad上的UIModalPresentationFormSheet.键盘出现时如何调整UITextView高度前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
UITextView是模态控制器视图的子视图.当键盘出现时,我需要降低UITextView高度,以便UITextView的底部边框y坐标等于键盘的顶部y坐标.
我想要键盘高度
CGRect frameBegin = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] ;
CGRect frameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

CGRect resultBegin = [self.view convertRect:frameBegin fromView:nil];
CGRect resultEnd = [self.view convertRect:frameEnd fromView:nil];

CGFloat kbdHeight = resultBegin.origin.y  - resultEnd.origin.y;

问题是当键盘出现时,此模态视图会跳起来.在这种情况下如何计算键盘的顶部边框坐标?

解决方法

你可以这样做:
1. Register for keyboard notifications:

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

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(myTextViewHeightAdjustMethod:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

2. Calculate intersection and adjust textView height with the bottom constraint

    - (void)myTextViewHeightAdjustMethod:(NSNotification *)notification
    {
        NSDictionary *userInfo = [notification userInfo];
        CGRect keyboardFinalFrame = [[userInfo emf_ObjectOrNilForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

        CGPoint keyboardOriginInView = [self.view convertPoint:keyboardFinalFrame.origin fromView:nil];

             CGFloat intersectionY = CGRectGetMaxY(self.view.frame) - keyboardOriginInView.y;

             if (intersectionY >= 0)
             {
                 self.textViewBottomConstraint.constant = intersectionY + originalTextViewBottomConstraint;

                 [self.textView setNeedsLayout];
    }

请记得取消注册通知.

原文链接:https://www.f2er.com/iOS/331689.html

猜你在找的iOS相关文章