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];
}
请记得取消注册通知.