为什么将我的viewcontroller添加到Navigationcontroller似乎会干扰我的约束? [英] Why does adding my viewcontroller to a navigationcontroller seem to interfere with my constraints?
问题描述
由于我有时有时很难从情节提要的布局中获得期望,所以我在程序限制方面玩得有点...我知道从这里开始会更加困难-但希望最终游戏用这种方法更有效。 ;-)
As I find it difficult sometimes to get what I expect from layouting with storyboards, I am playing around a bit with programmatic constraints... I am aware that getting started here will be more difficult – but hope the „end-game" with this approach is more efficient. ;-)
当我在简单的viewController&上使用以下代码时主视图和centerView组合一切均符合预期,即mainView填充整个可用屏幕,而CenterView仅填充mainView的中心部分:
When I use the following code on a simple viewController & mainView & centerView combo everything is as expected i.e. mainView fills entire screen available and centerView fills only the center part of mainView:
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(_centerView);
NSDictionary *metrics = @{@"padding":@15.0};
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-padding-[_centerView]-padding-|" options:0 metrics:metrics views:viewsDictionary]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-padding-[_centerView]-padding-|" options:0 metrics:metrics views:viewsDictionary]];
如果我将相同的viewController作为root添加到navigationController中(或定义另一个VC作为root和稍后再推送),它无法按预期工作:navBar下面的屏幕现在只是纯白色。
If I add the same viewController to an navigationController as root however (or define a different VC as root and push mine on later), it does not work as expected: the screen below the navBar is now just plain white.
我为所有视图上色,缺少的视图似乎是„从导航栏左上角的某个位置闪耀。为什么导航控制器完全干扰了上述布局约束?我的印象是,默认情况下,导航控制器的根视图控制器的主视图会填充导航栏下方窗口的剩余。还是我弄错了,需要在导航和mainView之间添加约束?还是我想念其他东西?
I colored all views and the missing ones seem to „shine through" a bit from the top left corner under the navigation bar. Why does the navigation controller „interfere" with the above layout constraints at all? I was under the impression that the main view of the root view controller of a navigation controller by default fills the „remainder" of the window below the navigation bar. Or am I mistaken and need to add constraints between navigation and my mainView? Or am I missing something else?
任何想法吗?
BTW .:如果有人知道一个好的教程或博客或其他有关以编程方式组成屏幕和屏幕问题的来源在它们之间进行导航(导航控制器,选项卡控制器,视图)和/或添加布局约束,我也会非常感兴趣。
BTW.: If someone knows a good tutorial or blog or other source that deals with the issue of programmatically composing screens & navigation between them (navigation controllers, tabbar controllers, views) and/or adding layout constraints I would be also very interested.
请多多感谢。
推荐答案
好的,经过更多测试,看来至少就我而言,我不应该设置view.translatesAutoresizingMaskIntoConstraints = NO;用于rootView。一旦将其设置为YES(仅用于此视图),它将再次起作用...
OK, after some more testing, it appears that at least in my case I shouldn't set view.translatesAutoresizingMaskIntoConstraints = NO; for the rootView. As soon as I set it to YES (for this view only) it works again...
这篇关于为什么将我的viewcontroller添加到Navigationcontroller似乎会干扰我的约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!