2

NSLayoutConstraintの間self.navigationcontroller.navigationbarに と ビューを追加できますかself.view。ここで self はUIViewControllerインスタンスで_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];
4

3 に答える 3

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 に答える