为什么我在使用砖石时无法立即获得框架?

时间:2022-02-14 21:10:17

This code:

这段代码:

[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
NSLog(@"%@", self.scrollView);

result is:<UIScrollView: 0x7faad400cc00; frame = (0 0; 0 0)

结果是: :0x7faad400cc00;>

However this code:

但是这段代码:

[self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    NSLog(@"%@", self.scrollView);
});

result is:<UIScrollView: 0x7ff8d1043200; frame = (0 0; 375 667);

结果是: :0x7ff8d1043200;>

Why I can't get the frame immediately but I can get after 0.1 second?

为什么我不能立即获得框架,但我可以在0.1秒后获得?

2 个解决方案

#1


1  

Masonry is a wrapper for autolayouts, and autolayouts calculate itself frame in - (void)layoutSubviews; method, and only after that u can get frames of all views.

Masonry是自动布局的包装器,自动布局自动计算框架 - (void)layoutSubviews;方法,只有在那之后你才能获得所有视图的帧。

masonry methods mas_makeConstraints and similar just setups Constraints no more.

砌体方法mas_makeConstraints和类似的设置不再有约束。

And if you need update constraints you must call mas_remakeConstraints: its just update constraits, for update Frames of views, we must call method setNeedsLayout for setup a flag about recalculation in next Display cycle, and if we want update frames immediately we must call layoutIfNeeded method.

如果你需要更新约束,你必须调用mas_remakeConstraints:它只是更新约束,对于更新视图帧,我们必须调用方法setNeedsLayout来设置关于下一个显示周期中重新计算的标志,如果我们想要立即更新帧,我们必须调用layoutIfNeeded方法。

#2


0  

Simply because you are changing your view's constraints/layout in a block. If you understand what a block does completely, you will know the answer to your question.

仅仅因为您正在更改块中的视图约束/布局。如果你完全了解一个块的作用,你就会知道你的问题的答案。

The program will run from the first line of your code, and go through the NSLog method line, and the codes inside the block will either be called first before the NSLog line ,Or after the NSLog line. As what you've said, you can get the frame after 0.1 second. Again that's because the codes inside the block finishes slower than the NSLog line.

程序将从代码的第一行运行,并通过NSLog方法行,块内的代码将在NSLog行之前或NSLog行之后首先调用。正如你所说,你可以在0.1秒之后得到帧。再次,这是因为块内的代码比NSLog行更慢。

#1


1  

Masonry is a wrapper for autolayouts, and autolayouts calculate itself frame in - (void)layoutSubviews; method, and only after that u can get frames of all views.

Masonry是自动布局的包装器,自动布局自动计算框架 - (void)layoutSubviews;方法,只有在那之后你才能获得所有视图的帧。

masonry methods mas_makeConstraints and similar just setups Constraints no more.

砌体方法mas_makeConstraints和类似的设置不再有约束。

And if you need update constraints you must call mas_remakeConstraints: its just update constraits, for update Frames of views, we must call method setNeedsLayout for setup a flag about recalculation in next Display cycle, and if we want update frames immediately we must call layoutIfNeeded method.

如果你需要更新约束,你必须调用mas_remakeConstraints:它只是更新约束,对于更新视图帧,我们必须调用方法setNeedsLayout来设置关于下一个显示周期中重新计算的标志,如果我们想要立即更新帧,我们必须调用layoutIfNeeded方法。

#2


0  

Simply because you are changing your view's constraints/layout in a block. If you understand what a block does completely, you will know the answer to your question.

仅仅因为您正在更改块中的视图约束/布局。如果你完全了解一个块的作用,你就会知道你的问题的答案。

The program will run from the first line of your code, and go through the NSLog method line, and the codes inside the block will either be called first before the NSLog line ,Or after the NSLog line. As what you've said, you can get the frame after 0.1 second. Again that's because the codes inside the block finishes slower than the NSLog line.

程序将从代码的第一行运行,并通过NSLog方法行,块内的代码将在NSLog行之前或NSLog行之后首先调用。正如你所说,你可以在0.1秒之后得到帧。再次,这是因为块内的代码比NSLog行更慢。