Ios NSLAYOUT导航栏和之间的约束;视图控制器视图

Ios NSLAYOUT导航栏和之间的约束;视图控制器视图,ios,objective-c,ios6,autolayout,Ios,Objective C,Ios6,Autolayout,我们是否可以在self.navigationcontroller.navigationbar和self.view中的视图之间添加NSLayoutConstraint。这里self是一个UIViewController实例,\u textField是self.view 我需要的是,无论navigationBar是否为半透明,UI都应该是相似的 我试过以下方法。但它不起作用 NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem

我们是否可以在
self.navigationcontroller.navigationbar
self.view
中的视图之间添加
NSLayoutConstraint
。这里self是一个
UIViewController
实例,
\u textField
self.view

我需要的是,无论
navigationBar
是否为半透明,UI都应该是相似的

我试过以下方法。但它不起作用

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:_textField
                                                      attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
                                                         toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0 constant:20];
[self.navigationcontroller.view addConstraint:cn];

在文本字段顶部和父视图顶部之间添加约束。约束的常量可以设置为状态栏的高度+导航栏的高度

显然,只有当状态栏和导航栏都是半透明的,并且视图控制器想要全屏布局时,下面的代码段才会起作用。如果需要,您可以轻松测试透明度并进行相应调整

如果您使用的是interface builder,还可以为现有约束创建一个IBOutlet,并将其设置为常量,而不是创建一个新约束

// Obtain the view rect of the status bar frame in either portrait or landscape
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect statusBarWindowRect = [self.view.window convertRect:statusBarFrame fromWindow: nil];
CGRect statusBarViewRect = [self.view convertRect:statusBarWindowRect fromView: nil];

// Add Status Bar and Navigation Bar heights together
CGFloat height = self.navigationController.navigationBar.frame.size.height +
statusBarViewRect.size.height;

// Create & Add Constraint
NSLayoutConstraint *constraint =
[NSLayoutConstraint constraintWithItem:self.fieldLabel
                             attribute:NSLayoutAttributeTop
                             relatedBy:0
                                toItem:self.view
                             attribute:NSLayoutAttributeTop
                            multiplier:1
                              constant:height];

[self.view addConstraint:constraint];

是的,您可以在导航栏和视图之间添加约束。添加到导航控制器的根视图控制器包含topLayoutGuide。因此,请按如下方式调整代码:

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:_textField
                                                      attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
                                                         toItem:self.rootViewController.topLayoutGuide attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0 constant:20];
[self.rootViewController.view addConstraint:cn];
请注意,我根本没有引用导航控制器,而是引用导航控制器的rootViewController


也可以使用bottomLayoutGuide以相同的方式在选项卡栏上方移动。(但是,如果您需要这样做,您将在iOS框架中遇到一个bug,这里有一个变通补丁:)

请查看上的
topLayoutGuide
属性

苹果的文档中有一个“UIViewController”的例子是这样的

topLayoutGuide
Indicates the highest vertical extent for your onscreen content, for use with Auto Layout constraints. (read-only)

@property(nonatomic, readonly, retain) id<UILayoutSupport> topLayoutGuide

控制台中有错误消息吗?@Sj.,你有没有发现这个消息?在这里有同样的问题,到处搜索。@Pat花了一段时间后,我放弃了这种方法。如果您发现任何解决方案,请与我们分享。谢谢
[button setTranslatesAutoresizingMaskIntoConstraints: NO];
id topGuide = myViewController.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (button, topGuide);
[myViewController.view addConstraints:
    [NSLayoutConstraint constraintsWithVisualFormat: @"V: [topGuide]-20-[button]"
                                                 options: 0
                                                 metrics: nil
                                                   views: viewsDictionary]
self.view layoutSubviews; // You must call this method here or the system raises an exception
];