当键盘出现时,将UITextField和UITableView向上移动

时间:2014-02-16 09:32:06

标签: ios objective-c uitableview uitextfield uinavigationbar

我有一个视图1.导航栏2.UITableView 3. UITextView。

当我开始编辑textView时,会出现一个键盘,我需要为TextView和TableView设置动画。我已经实现了:https://stackoverflow.com/a/8704371/1808179,但是整个视图都会动画,覆盖导航栏。

我尝试单独为textView设置动画,如:

- (void)keyboardWillShow:(NSNotification*)notification
{
    CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-218,chatTextField.frame.size.width,chatTextField.frame.size.height);
    [UIView animateWithDuration:0.5 animations:^{ chatTextField.frame = chatTextFieldFrame;}];
}

但它没有动画,也不会在TableView旁边同步动画。

在不覆盖导航栏的情况下,为tableView和textView设置动画的最佳方法是什么?

3 个答案:

答案 0 :(得分:6)

我在解决此问题时通常会使用以下代码段。

使用UITableView(它只是UIScrollView的子类),您应该设置contentInsets,而不是每次只更改框架。这在使用半透明键盘的iOS7中特别好。

- (void)viewDidLoad;
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)dealloc;
{
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - Keyboard Notifications

- (void)keyboardWillShow:(NSNotification *)notification;
{
  NSDictionary *userInfo = [notification userInfo];
  NSValue *keyboardBoundsValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
  CGFloat keyboardHeight = [keyboardBoundsValue CGRectValue].size.height;

  CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
  UIEdgeInsets insets = [[self tableView] contentInset];
  [UIView animateWithDuration:duration delay:0. options:animationCurve animations:^{
    [[self tableView] setContentInset:UIEdgeInsetsMake(insets.top, insets.left, keyboardHeight, insets.right)];
    [[self view] layoutIfNeeded];
  } completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)notification;
{
  NSDictionary *userInfo = [notification userInfo];
  CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
  UIEdgeInsets insets = [[self tableView] contentInset];
  [UIView animateWithDuration:duration delay:0. options:animationCurve animations:^{
    [[self tableView] setContentInset:UIEdgeInsetsMake(insets.top, insets.left, 0., insets.right)];
    [[self view] layoutIfNeeded];
  } completion:nil];
}

答案 1 :(得分:0)

如果有人想知道,我就这样做了:

- (void)keyboardWillShow:(NSNotification*)notification
{
    CGRect chatTableViewFrame = CGRectMake(0,65,320,chatTableView.frame.size.height-180);
    [UIView animateWithDuration:0.3 animations:^{ chatTableView.frame = chatTableViewFrame;}];

    CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-170,chatTextField.frame.size.width,chatTextField.frame.size.height);
    [UIView animateWithDuration:0.3 animations:^{ chatTextField.frame = chatTextFieldFrame;}];


    [self.chatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.chat.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

反之亦然,适用于keyboardWillHide。

答案 2 :(得分:-1)

将此行写在所需的事件中。

PdfWriter writer= PdfWriter.getInstance(document,new FileOutputStream(Result)) 
writer.setInitialLeading(16);