Sj.*_*Sj. 2 objective-c ios autolayout ios6 nsautolayout
我们可以NSLayoutConstraint在self.navigationcontroller.navigationbar里面添加一个视图和一个视图self.view.这里self是一个UIViewController实例,_textField是一个子视图self.view
我需要的是UI看起来应该是相似的,无论是否navigationBar是半透明.
我尝试了以下内容.但它不起作用.
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];
Run Code Online (Sandbox Code Playgroud)
是的,您可以在导航栏和视图之间添加约束.添加到导航控制器的根视图控制器包含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];
Run Code Online (Sandbox Code Playgroud)
请注意,除了导航控制器的rootViewController之外,我根本没有引用导航控制器.
您也可以使用bottomLayoutGuide以同样的方式在TabBar之上.(但是如果你需要这样做,你会在iOS框架中遇到一个带有解决方法补丁的错误:UIViews在标签栏下面结束)
查看UIViewControllertopLayoutGuide上的属性.
在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
Run Code Online (Sandbox Code Playgroud)
然后...
作为如何以编程方式将此属性与自动布局一起使用的示例,假设您要将控件放置在顶部布局指南下方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
];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11559 次 |
| 最近记录: |