我有一个带有UITextView的UIView,里面有2个按钮。当用户想要评论帖子时,视图会移动到键盘上方(这样可以正常工作)。
当用户开始输入文本时,UIView和UITextView将相应增长到内容。这适用于除iPod和iPhone 4S之外的所有设备。在这些设备上,textView和UIView在键盘后面增长。我试过改变垂直内容压缩阻力优先级,但无济于事。
iPhone 4s:
iPhone 7:
代码的时间:
- (void)textViewDidChange:(UITextView *)textView {
[self updateInputPanel];
}
- (void)updateInputPanel {
__weak __typeof__(self) weakSelf = self;
dispatch_async (dispatch_get_main_queue (), ^{
__typeof__(self) strongSelf = weakSelf;
CGFloat widthText = textInput.frame.size.width, heightText;
CGSize sizeText = [textInput sizeThatFits:CGSizeMake (widthText, MAXFLOAT)];
heightText = fmaxf (36.5, sizeText.height);
heightText = fminf (110.0, heightText);
CGFloat heightInput = heightText + (51.0 - 36.5);
CGRect frameViewInput = viewInput.frame;
frameViewInput.origin.y = heightView - heightInput;
frameViewInput.size.height = heightInput;
viewInput.frame = frameViewInput;
[viewInput layoutIfNeeded];
CGRect frameTextInput = textInput.frame;
frameTextInput.size.height = heightText;
textInput.frame = frameTextInput;
CGPoint offset = CGPointMake (0, sizeText.height - heightText);
[textInput setContentOffset:offset animated:NO];
[strongSelf scrollToBottom:NO];
});
}
- (void)keyboardShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
CGRect keyboard = [[info valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval duration = [[info valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGFloat heightKeyboard = keyboard.size.height;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
self.view.center = CGPointMake (centerView.x, centerView.y - heightKeyboard);
}
completion:nil];
}