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 约束代码
				
        
 
            
        - SetOnItemSelectedListener上的微调程序错误 2022-01-01
 - 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
 - GPS状态的广播接收器? 2022-01-01
 - 网上有没有好的 UIScrollView 教程? 2022-01-01
 - Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
 - 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
 - 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
 - URL编码Swift iOS 2022-01-01
 - UITextView 内容插图 2022-01-01
 - 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
 
