【发布时间】:2014-03-06 11:59:30
【问题描述】:
我正在使用 UITableView (chatTable) 以及 UITabBar (chatTabBar) 和 imageView 中的一个 textField。我正在使用自动布局。当键盘出现和消失时,我使用以下代码更改视图。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// get animation info from userInfo
NSTimeInterval animationDuration;
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
// resize the frame
[UIView animateWithDuration:animationDuration delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.keyboardHeight.constant = keyboardFrame.size.height - TABBAR_HEIGHT ;
[self.view layoutIfNeeded];
} completion:nil];
if ([chatData count] != VALUE_ZERO)
{
[chatTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([chatData count] - VALUE_ONE) inSection:VALUE_ZERO] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
}
- (void)keyboardWillHide:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
// get animation info from userInfo
NSTimeInterval animationDuration;
CGRect keyboardFrame;
[[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
[[info objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardFrame];
// Set view frame
[UIView animateWithDuration:animationDuration delay:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.keyboardHeight.constant -= keyboardFrame.size.height - TABBAR_HEIGHT;
[self.view layoutIfNeeded];
} completion:nil];
}
现在,当我按下回车键时,tableview 会上升一点(从屏幕 2 到屏幕 3)。 keyboardHeight 是 tabBar 和主视图之间的底部空间约束。
(屏幕 2)
(屏幕3)
我已经尝试了很多东西,但我无法找到为什么 tableview 会上升一段时间。 (问题是没有流畅的动画。)(注意:我将延迟设置为 2.0 只是为了显示以下屏幕截图(屏幕 3)中发生的情况,否则它的值为 0)
【问题讨论】:
标签: ios objective-c uitableview autolayout