[英]UIModalPresentationFormSheet on iPad. How to adjust UITextView height when keyboard appears
UITextView is subview of the modal controller view. UITextView是模态控制器视图的子视图。 I need to decrease UITextView height when keyboard appears in order to bottom border y coordinate of the UITextView to be equal to keyboard's top y coordinate.
我需要在键盘出现时降低UITextView高度,以使UITextView的底部边框y坐标等于键盘的顶部y坐标。 I'getting keyboard height
我想要键盘高度
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;
The problem is that this modal view jumps up when keyboard appears. 问题是当键盘出现时,此模态视图会跳起来。 How to calculate keyboard's top border coordinate in this case?
在这种情况下如何计算键盘的顶部边框坐标?
You could do this: 你可以这样做:
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];
}
Remember to unregister for notifications. 请记得取消注册通知。
If you don't need to write the code for this yourself I recommend using https://github.com/hackiftekhar/IQKeyboardManager 如果您不需要自己编写代码,我建议使用https://github.com/hackiftekhar/IQKeyboardManager
It works great (for me, so far) and all you need to do is import it and add this one line of code in AppDelegate: 它工作得很好(对我来说,到目前为止),你需要做的就是导入它并在AppDelegate中添加这一行代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Magic one-liner
IQKeyboardManager.sharedManager().enable = true
return true
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.