2

我们可以NSLayoutConstraint在. 这里 self 是一个实例,是self.navigationcontroller.navigationbarself.viewUIViewController_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];
4

3 回答 3

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 under tab bar

于 2014-03-06T04:38:38.173 回答
5

Check out the topLayoutGuide property on UIViewController.

There's an example in Apple's doc for `UIViewController' that goes like this...

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

And then...

As an example of how to programmatically use this property with Auto Layout, say you want to position a control such that its top edge is 20 points below the top layout guide. This scenario applies to any of the scenarios listed above. Use code similar to the following:

[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
];
于 2014-08-14T06:27:12.333 回答
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];
于 2013-07-20T03:50:16.807 回答