导航栏和导航栏之间的NSLayoutConstraint ViewControllers视图

时间:2013-06-12 20:54:43

标签: ios objective-c ios6 autolayout nsautolayout

我们可以在NSLayoutConstraintself.navigationcontroller.navigationbar内的视图之间添加self.view吗? self是UIViewController个实例,_textFieldself.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];

3 个答案:

答案 0 :(得分:6)

是的,您可以在导航栏和视图之间添加约束。添加到导航控制器的根视图控制器包含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以同样的方式在TabBar上方。 (但是如果你需要这样做,你会在iOS框架中遇到一个带有解决方法补丁的错误:UIViews ending up beneath tab bar

答案 1 :(得分:5)

查看UIViewController上的topLayoutGuide媒体资源。

Apple的'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

然后......

  

作为如何以编程方式将此属性与Auto一起使用的示例   布局,假设您想要将控件放置在其上边缘   顶部布局指南下方20点。此方案适用于任何   上面列出的方案。使用类似于以下内容的代码:

[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
];

答案 2 :(得分:3)

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

显然,以下代码段仅在状态栏和导航栏都是半透明且视图控制器需要全屏布局时才有效。如有必要,您可以轻松测试透明度并进行相应调整。

如果您正在使用界面构建器,您还可以为现有约束创建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];
相关问题