【问题标题】:ViewController's UILayoutAnchor messed up when added to UINavigationControllerViewController 的 UILayoutAnchor 添加到 UINavigationController 时搞砸了
【发布时间】:2017-07-14 05:32:31
【问题描述】:

我正在玩 UILayoutAnchor,它与我的 ViewController 完美配合。但是当我将 ViewController 添加到 NavigationController 时,我没有得到我期望的结果。代码如下:

UIButton *topContainer = [UIButton buttonWithType:UIButtonTypeCustom];
[topContainer setBackgroundColor:[UIColor redColor]];
[self.view addSubview:topContainer];
//Setup anchor
topContainer.translatesAutoresizingMaskIntoConstraints = false;
[topContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = true;
[topContainer.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor].active = true;
[topContainer.heightAnchor constraintEqualToConstant:40].active = true;
[topContainer.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor constant:0].active = true;

以上代码的结果:

现在,当我将相同的 ViewController 作为 NavigationController 的根时(通过我也有意隐藏的情节提要),相同的代码会给出以下结果。

我首先注意到的是从灰色变为黑色的背景。视图控制器上的交互也不再起作用。我检查了导航控制器的检查器,并检查了它的“用户交互启用”。并且 topContainer(redbar) 没有延伸到屏幕边缘。

我尝试过使用 viewcontrollers readableContentGuide,但它一直延伸到边缘。请参阅下面的代码以及结果。

[topContainer.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor].active = true;
[topContainer.leadingAnchor constraintEqualToAnchor:self.view.readableContentGuide.leadingAnchor].active = true;

我还尝试显示 NavigationController 和一个干净的构建,但它仍然给出相同的结果。我哪里搞砸了?

【问题讨论】:

    标签: ios objective-c uiviewcontroller uinavigationcontroller


    【解决方案1】:

    泰迪熊编程确实有效。问题出在我的代码中某处有这一行。

    self.view.translatesAutoresizingMaskIntoConstraints = false;
    

    我只是通过评论/删除它得到了我想要的结果。我把它留在这里以防万一有人遇到它。

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 1970-01-01
      • 2011-08-17
      • 2010-09-05
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多