2014-02-16 96 views
1

我有一个视图1.导航栏2.UITableView 3.UITextView。当键盘出现时将UITextField和UITableView向上移动

当我开始编辑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没有覆盖导航栏?

+0

您是否尝试更改表高而非原点? – Wain

+0

@Wain这似乎工作。谢谢! – Spenciefy

回答

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]; 
} 
+0

我应该如何处理TextField? – Spenciefy

+0

我认为在桌面视图中会自动滚动它,对吗?如果没有,你可以抓住它的框架,并在tableView上手动调用'scrollToRect'。 – petehare

+0

textView与tableView分开 - 它位于 – Spenciefy

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。

-1

在希望的事件中写下此行。

self.view.frame = [[UIScreen mainScreen] applicationFrame]; 
相关问题