更新约束不会更改框架

时间:2014-07-08 11:56:32

标签: ios objective-c nslayoutconstraint

出于某种原因,当我改变约束的常数时,它不会改变帧。

我读过的大多数帖子建议在更改约束后调用layoutIfNeeded,但这对我来说似乎不起作用。

我的视图层次结构如下所示:

self.parentViewController.view
   self.view
     self.contentView
        a bunch of text fields here

self.view被添加为parentViewController.view上的子视图,因为它需要与UIAlertView类似。 (Self也被添加为childViewController)。

我在topLayoutGuideself.contentView.top之间有一个约束,它有一个名为topConstraint的商店。

显示键盘时,执行以下方法:

- (void)keyboardWillShow:(NSNotification*)notification {
    //Currently using a constant value and no animation for debugging.
    [self.topConstraint setConstant:20];
    [self.view setNeedsUpdateConstraints];
    [self.view layoutIfNeeded];
}

我确保keyboardWillShow已执行,并且我已在self.topConstraint方法中打印keyboardWillHide,并且常量值设置为20但尚未更改任何帧。< / p>

知道问题可能是什么?

编辑:非常有趣,如果我旋转设备,self.contentView的框架更改。

编辑:正如所建议的那样,我尝试从代码中添加约束,但仍然没有用。 (除了constantHeight和self.contentView的constantWidth之外,我删除了storyboard中的所有约束):

self.verticalSpace = [NSLayoutConstraint constraintWithItem:self.contentView
                                                  attribute:NSLayoutAttributeTop
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:self.view
                                                  attribute:NSLayoutAttributeTop
                                                 multiplier:1.0f
                                                   constant:20];
[self.view addConstraint:self.verticalSpace];
[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];

Edit2:目前self.parentViewController继承自ECSlidingViewController,但如果我将其更改为UIViewController,它将按预期工作。

1 个答案:

答案 0 :(得分:1)

出于某种原因,如果我的视图控制器继承了ECSlidingViewController,Autolayout无法正常工作,无论我做了什么。

相反,我制作了InitialViewController作为属性的ECSlidingViewController,我将其视图作为子视图添加到InitialViewController的视图中:

[self.view addSubview:self.slidingViewContoller.view];
[self addChildViewController:self.slidingViewContoller];

所以我的视图层次结构现在看起来像这样:

initialViewController.view
   slidingViewController.view

当我添加self.view时,我将其添加到initialViewController.view,而不是将其添加到slidingViewController.view

之后,我的视图层次结构如下:

initialViewController.view
   slidingViewController.view
   self.view
      self.contentView
        textFields