Where to add topLayoutGuide constraint code(在哪里添加 topLayoutGuide 约束代码)
问题描述
想了个办法,把下面的代码放到我的子类导航控制器.m文件的viewDidLoad方法中:
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
}
}
原帖
Apple 的 doc 没有说清楚我应该把这段代码放在哪里(哪个类,哪个方法)(不知道 self 指的是什么代码):
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
推荐答案
在这种情况下,self 可以引用视图控制器.使用此代码,您正在为其视图添加约束,因此它可以在调用 layoutSubviews 时设置子视图来布局子视图.如果您在 viewDidLoad 方法中添加此代码(我建议您在其中添加),您可以将出现的 myViewController 替换为 self
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 约束代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在哪里添加 topLayoutGuide 约束代码
基础教程推荐
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
