【发布时间】: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