本文介绍了在哪里添加 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 约束代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:18