凡添加topLayoutGuide约束code [英] Where to add topLayoutGuide constraint code
问题描述
解决方案
想出一个解决方案,把我的子类导航控制器的 viewDidLoad中
方法如下code .m文件:
- (无效)viewDidLoad中{
[超级viewDidLoad中];
//在载入视图后的任何额外的设置。 如果([[[的UIDevice currentDevice] systemVersion]的floatValue]≥= 7.0f){
[自拍] setTranslatesAutoresizingMaskIntoConstraints:NO]; ID topGuide = [个体经营topLayoutGuide]
UIView的* selfView = [自拍]
*的NSDictionary = viewsDictionary NSDictionaryOfVariableBindings(selfView,topGuide);
[[[自拍]窗口] addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@V:[topGuide] -0- [selfView]
选项:0
指标:无
观点:viewsDictionary]
];
[[[自拍]窗口] layoutSubviews] //你必须在这里调用此方法或系统会引发异常
}
}
原贴
苹果公司<一href=\"https://developer.apple.com/LIBRARY/IOS/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instp/UIViewController/topLayoutGuide\"相对=nofollow>商务部没有说清楚,在哪里(哪一类,哪一种方法),我应该把code的这个块(不知道是什么做自
指的是在code):
[按钮setTranslatesAutoresizingMaskIntoConstraints:NO];
ID topGuide = myViewController.topLayoutGuide;
*的NSDictionary = viewsDictionary NSDictionaryOfVariableBindings(按钮,topGuide);
[myViewController.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@V:[topGuide] -20- [按钮]
选项:0
指标:无
观点:viewsDictionary]
self.view layoutSubviews; //你必须在这里调用此方法或系统会引发异常
];
我觉得,code以上块有一些错字,所以这里就是我认为它应该是:
[按钮setTranslatesAutoresizingMaskIntoConstraints:NO];
ID topGuide = myViewController.topLayoutGuide;
*的NSDictionary = viewsDictionary NSDictionaryOfVariableBindings(按钮,topGuide);
[myViewController.view addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@V:[topGuide] -20- [按钮]
选项:0
指标:无
观点:viewsDictionary]
];
self.view.layoutSubviews; //你必须在这里调用此方法或系统会引发异常
在这种情况下,自
可引用视图控制器。有了这个code,你添加约束其观点,因此可以布局它的子视图,你叫 layoutSubviews
时设置。如果您在 viewDidLoad中
方法添加此code(我推荐你增加它那里),你可以替换 myViewController $的出现C $ C>按
自
Solution
Figured out a solution, put the following code in the viewDidLoad
method of my subclassed navigation controller .m file:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0f) {
[[self view] setTranslatesAutoresizingMaskIntoConstraints:NO];
id topGuide = [self topLayoutGuide];
UIView * selfView = [self view];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (selfView, topGuide);
[[[self view] window] addConstraints:
[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide]-0-[selfView]"
options:0
metrics:nil
views:viewsDictionary]
];
[[[self view] window] layoutSubviews]; // You must call this method here or the system raises an exception
}
}
Original Post
Apple's doc didn't say it clear that where (which class, which method) should I put this chunk of code (don't know what does self
refers to in the code):
[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
];
And I feel that the above chunk of code has some typo, so here's what I think it should be:
[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
In that case, self
can refer to a view controller. With this code, your are adding constraint to its view, so it can layout it subviews as you set them when calling layoutSubviews
. If you add this code in the viewDidLoad
method (and I recommand you to add it there) you can replace occurrences of myViewController
by self
这篇关于凡添加topLayoutGuide约束code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!