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;
Run Code Online (Sandbox Code Playgroud)
问题是当键盘出现时,此模态视图会跳起来.在这种情况下如何计算键盘的顶部边框坐标?
小智 4
你可以这样做:
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];
}
Run Code Online (Sandbox Code Playgroud)
请记住取消注册通知。
| 归档时间: |
|
| 查看次数: |
752 次 |
| 最近记录: |