【问题标题】:How to move view when keyboard appear键盘出现时如何移动视图
【发布时间】:2014-07-26 06:21:07
【问题描述】:

我有多个UITextFields,当键盘出现时我需要向上移动视图。但我只需要 3 个底部字段,并且不想移动顶部其他字段的视图。我使用这个代码,但是当用户点击每个字段时它的移动视图,而不是当用户点击 2 个底部字段时我才需要移动视图

- (void)viewWillAppear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

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

#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = -115.0f;  //set the -35.0f to your required value
        self.view.frame = f;
    }];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = 0.0f;
        self.view.frame = f;
    }];
}

【问题讨论】:

标签: ios uitextfield


【解决方案1】:

您可以在 UITextfield 委托方法 textFieldDidBeginEditing 中更改任何 UIViews、UIButton.... 的框架:当您点击文本字段时,此委托将调用

 - (void)textFieldDidBeginEditing:(UITextField *)textField{ 

      //here you will get the selected textfield
      //check whether the textfield is equal to any of three bottom field
       if(textfield==BottomTextfield)
       {
            //here you can change the frame of the UIViews
       }
   }

【讨论】:

    【解决方案2】:

    您需要使用UITextField's 委托方法更改UIView's 框架:

    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        int tag = textField.tag;
        int section = tag / 1000;
        tag -= 1000 * section;
        section -= 1;
    
        CGPoint point = [[textField superview] convertPoint:textField.center toView:self.view];
    
    
        CGPoint contentOffset = tableView.contentOffset;
    
        if (IOS_7) {
            contentOffset.y += (point.y - 230);
            // Adjust this value as you need
    
        }
        else{
            contentOffset.y += (point.y - 150); // Adjust this value as you need
    
        }
        currentY = contentOffset.y;
        [tableView setContentOffset:contentOffset animated:YES];
    
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
       [tableView setContentOffset:CGPointMake(0, 0)];
    }
    

    希望这会有所帮助...!!!

    【讨论】:

      【解决方案3】:

      您可以检查第一响应者(活动文本字段)。这个 SO 答案显示了如何做到这一点:https://stackoverflow.com/a/1823360/2486394。并且仅移动某些文本字段的视图。

      【讨论】:

        猜你喜欢
        • 2010-12-19
        • 2018-05-21
        • 2016-05-07
        • 1970-01-01
        • 2015-11-29
        • 2012-07-25
        • 2023-03-08
        • 2021-01-31
        相关资源
        最近更新 更多