【发布时间】:2013-12-06 18:05:55
【问题描述】:
解决方案
想了个办法,把下面的代码放到我的子类导航控制器.m文件的viewDidLoad方法中:
- (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
}
}
原帖
苹果的doc没有说清楚我应该把这段代码放在哪里(哪个类,哪个方法)(不知道self在代码中指的是什么):
[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
];
而且我觉得上面那段代码有错别字,所以我认为应该是这样的:
[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
【问题讨论】:
-
我建议您将其作为方法添加到 UIButton 上的某个类别中。例如
UIButton+CustomLayOut& 然后调用它。但是,您也可以在didFinishLaunchingWithOptions中的appDelegate类中实现
标签: ios autolayout