我在玩UILayoutAnchor,它和我的ViewController完美配合。但是當我將ViewController添加到NavigationController時,我沒有得到我期待的結果。下面的代碼:ViewController的UILayoutAnchor在添加到UINavigationController時搞砸了
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和一個乾淨的構建,但它仍然給出了同樣的結果。我在哪裏搞砸了?