我有一个带有主视图的UIViewController。在该视图中,我有一个内部有UITextView的红色UIView,我希望在写入文本时垂直增长。如果redView最初在屏幕中间的某个地方启动,并且只是通过修改我创建的UITextViewHeightConstraint而弹出键盘,那么我能够有效地完成这项工作。
-(void)textViewDidChange:(UITextView *)textView {
[self.textView setScrollEnabled:NO];
CGSize newSize = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, 999)];
if (newSize.height > textView.frame.size.height) {
self.textViewHeight.constant = newSize.height;
}
}
当我将包含UITextView子视图的redView停靠在屏幕底部时,我的代码将使用以下代码恰当地将视图置于键盘上方:
-(void)adjustViewForKeyboardNotification:(NSNotification *)notification {
NSDictionary *notificationInfo = [notification userInfo];
CGRect finalKeyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
//Get AnimationCurve and AnimationDuration for keyboard popping up.
UIViewAnimationCurve animationCurve = (UIViewAnimationCurve) [[notificationInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
finalKeyboardFrame = [self.view convertRect:finalKeyboardFrame fromView:self.view.window];
CGRect textBarFrame = self.redView.frame;
textBarFrame.origin.y = finalKeyboardFrame.origin.y-textBarFrame.size.height;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
self.redView.frame = textBarFrame;
[UIView commitAnimations];
}
然而,当我开始输入并且UITextView扩展时,redView被发送回来并停靠在屏幕的底部(在键盘下) - 然后它在那里垂直扩展,因为我希望。我尝试过多种方式调整约束,但还没有找到解决方案。我已经尝试手动设置redView的框架,但这似乎也不起作用(可能是因为启用了自动布局)。
非常感谢任何建议或提示!