UIModalPresentationFormSheet on iPad. How to adjust UITextView height when keyboard appears
UITextView 是模态控制器视图的子视图。当键盘出现时,我需要降低 UITextView 的高度,以便 UITextView 的底部边框 y 坐标等于键盘的顶部 y 坐标。
我正在获取键盘高度
1
2 3 4 5 6 7 |
CGRect frameBegin = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] ;
CGRect frameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect resultBegin = [self.view convertRect:frameBegin fromView:nil]; CGFloat kbdHeight = resultBegin.origin.y – resultEnd.origin.y; |
问题是当键盘出现时这个模态视图会跳起来。这种情况下如何计算键盘的上边框坐标?
你可以这样做:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
1. Register for keyboard notifications:
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self 2. Calculate intersection and adjust textView height with the bottom constraint – (void)myTextViewHeightAdjustMethod:(NSNotification *)notification CGPoint keyboardOriginInView = [self.view convertPoint:keyboardFinalFrame.origin fromView:nil]; CGFloat intersectionY = CGRectGetMaxY(self.view.frame) – keyboardOriginInView.y; if (intersectionY >= 0) [self.textView setNeedsLayout]; |
记得取消注册通知。
如果您不需要自己编写代码,我建议使用 https://github.com/hackiftekhar/IQKeyboardManager
它工作得很好(对我来说,到目前为止),你需要做的就是导入它并在 AppDelegate 中添加这一行代码:
1
2 3 4 5 6 7 |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//Magic one-liner return true |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268704.html