【问题标题】:iOS ViewController subview moving when keyboard is presented出现键盘时iOS ViewController子视图移动
【发布时间】:2015-12-03 23:57:13
【问题描述】:

我有一个UITableView,它是我的视图控制器的一个属性。此视图控制器使用自动布局 + 故事板约束位于屏幕顶部下方 40 像素处。我最近添加了一个标签和文本字段,它们最初隐藏在情节提要中,并被限制在表格视图的正后方。

我添加了显示这些字段的方法:

-(void)showNeighborhoodFilter { [UIView animateWithDuration:.5 animations:^{ if (self.neighborhoodLabel.hidden) { self.neighborhoodLabel.hidden=NO; self.neighborhoodSelector.hidden=NO; self.tableView.frame = CGRectMake(0, self.tableView.frame.origin.y+40, self.tableView.frame.size.width, self.tableView.frame.size.height-40); } else{ self.neighborhoodLabel.hidden=YES; self.neighborhoodSelector.hidden=YES; self.tableView.frame = CGRectMake(0, self.tableView.frame.origin.y-40, self.tableView.frame.size.width, self.tableView.frame.size.height+40); } }]; }

这似乎工作正常,除非当我点击文本字段时,它将表格视图移回隐藏文本字段上方。我不知道发生了什么。

【问题讨论】:

  • 如果您使用约束,为什么要编辑框架?

标签: ios objective-c iphone uitableview autolayout


【解决方案1】:

为了改变文本文件的高度,在你的控制器中声明 NSLayoutConstraint 变量约束。

在 .h 文件中:

    __weak IBOutlet NSLayoutConstraint *heightConstraintOfMyView;//height of the view
    __weak IBOutlet NSLayoutConstraint *verticalspaceConstraintOfMyViewWithTopView;//space between myview and the view just above it
    __weak IBOutlet NSLayoutConstraint *verticalSpaceConstraintOfMyViewWithBottomView;//space between myview and the view just below it

在您的故事板中

已连接

使用约束改变框架。

if (myConditionTrueForShowingView) {
        //setting the constraint to update height 
        heightConstraintOfMyView = 40;
        verticalspaceConstraintOfMyViewWithTopView.constant=20;
        verticalSpaceConstraintOfMyViewWithBottomView.constant=80;
     }
  else{
        //setting the constraint to update height 
        heightConstraintOfMyView = 0;
        verticalspaceConstraintOfMyViewWithTopView.constant=20;
        verticalSpaceConstraintOfMyViewWithBottomView.constant=0;
     }

您还可以注册键盘通知。

- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

并在通知回调中处理高度变化-

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableBottomSpaceConstrain;

-(void)keyboardWasShown:(NSNotification*)aNotification
{
    self.view.translatesAutoresizingMaskIntoConstraints = YES;
    if (self.tableBottomSpaceConstrain.constant) {//
        return;
    }
    self.tableBottomSpaceConstrain.constant = 216;// Default Keyboard height is 216, varies for third party keyboard
    [self.view setNeedsUpdateConstraints];
    [self.view updateConstraints];
    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];

    [self.view setNeedsUpdateConstraints];
}

- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    self.tableBottomSpaceConstrain.constant =0;
    [self.view layoutIfNeeded];
    [self.view setNeedsLayout];
    [self.view updateConstraints];
}

了解更多关于 NSLayoutConstraint Implementing AutoLayout Constraints in Code

Apple Doc

如果 textfield 是 tableviewTPKeyboardAvoiding 的一部分,您可以使用一些第三方代码

查看此链接:How to make a UITextField move up when keyboard is present?

Making a UITableView scroll when text field is selected

【讨论】:

    【解决方案2】:

    如果您使用自动布局,则无法移动具有约束的视图,只要 iOS 决定重新计算约束,它将再次移动。

    您需要更改约束(您可以更改常量),或启用/禁用约束。

    【讨论】:

    • 我讨厌约束。在 iOS 6 上,视图件在代码中更易于管理。
    猜你喜欢
    • 2015-11-29
    • 2010-12-19
    • 2018-05-21
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2012-07-25
    相关资源
    最近更新 更多