键盘使用uiTextfield上下移动许多子视图

时间:2016-11-23 13:36:36

标签: ios objective-c iphone

我需要上下移动键盘。我有很多uiTextfields的子视图。这些子视图有一个superview,这个superview在滚动视图中。

我无法使用以下代码向上移动视图:

- (void) keyboardWasShown:(NSNotification *)notification{
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0);
    self.Scroll_view.contentInset = contentInsets;
    self.Scroll_view.scrollIndicatorInsets = contentInsets;
}

- (void) keyboardWillBeHidden:(NSNotification *)notification{
   UIEdgeInsets contentInsets = UIEdgeInsetsZero;
   self.Scroll_view.contentInset = contentInsets;
   self.Scroll_view.scrollIndicatorInsets = contentInsets;
}

当我将所有UITextfields放入ScrollView(不进入子视图)时,此代码工作正常,但我需要处理子视图并同时上下移动键盘。 如何在按下键盘下一个/返回键的同时上下移动键盘?

2 个答案:

答案 0 :(得分:0)

viewDidLoad方法

- (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)keyboardWillHide:(NSNotification *)aNotification
{
     // the keyboard is hiding reset the table's height
     NSTimeInterval animationDuration =
     [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
     CGRect frame = self.view.frame;
     frame.origin.y += 150;
     [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
     [UIView setAnimationDuration:animationDuration];
     self.view.frame = frame;
     [UIView commitAnimations];
 }

- (void)keyboardWillShow:(NSNotification *)aNotification

{
     // the keyboard is showing so resize the table's height
     NSTimeInterval animationDuration =
     [[[aNotification userInfo]     objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
     CGRect frame = self.view.frame;
     frame.origin.y -= 150;
     [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
     [UIView setAnimationDuration:animationDuration];
     self.view.frame = frame;
     [UIView commitAnimations];

}

答案 1 :(得分:0)

试试这个:

- (void)animateViewUpToTargetYOrigin:(CGFloat)yValue
{

    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidChangeFrameNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        CGFloat yOrigin = 0;
        CGFloat keyboardYOrigin = [[[note userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
        if (yValue > keyboardYOrigin) {
            yOrigin = yValue - keyboardYOrigin;
        }
        [UIView animateWithDuration:0.3f animations:^{

                self.view.frame = CGRectMake(self.frame.origin.x, -yOrigin, self.frame.size.width, self.frame.size.height);


        }];


}];

使用此代码,您只需要在textfield didbeginediting中调用此方法,并将yOrigin(textfield)+ height(textField)作为y值传递给它。 如果textFields在Scrollview中,或者任何子视图在发送Y值之前转换其相对于self.view的帧值。

E.g:

CGRect textFieldRectWithRespectToSelfView = [textField.superview convertRect:textField.frame toView:self.view];
[self.view animateViewUpToTargetYOrigin: textFieldRectWithRespectToSelfView.orgin.y + textFieldRectWithRespectToSelfView.size.height];

当您致电[textField resignFirstResponder]时,self.view会回到原来的位置。发生这种情况是因为UIKeyboardDidChangeFrameNotification始终一直在监听键盘框架的更改。

我在github上有一个演示项目作为示例请参考:

https://github.com/subhajitregor/ViewMoveUpExample