为什么在设置视图的框架时,layoutSubviews 不会自动在视图上调用?
我有一个看法。它是一个大型而复杂的项目的一部分。当我的视图的框架设置时,不会自动调用layoutSubviews。
对此,我尝试将以下方法放入我的视图中:
-(void) setFrame: (CGRect) frame {
NSLog (@"setting frame");
[super setFrame: frame];
[self setNeedsLayout];
}
此时,如预期的那样,在设置框架时重做了布局。如果我注释掉 setNeedsLayout 行,布局就不会完成。此外,当我在调用 super 的行单击“步入”时,调试器不会步入任何内容。据我所知, super 的 setFrame 方法没有被重写。
FWIW,我还做了一个小型测试项目,其中我更改了视图的框架。在本例中,当我这样做时,layoutSubviews 被调用。 [setNeedsLayout 在我的测试项目中没有被调用。] 所以我的大项目中的某些东西正在改变行为。但我不知道那东西是什么。
谁能解释一下可能发生了什么?
编辑:澄清一下,我的框架更改正在更改视图的高度。宽度没有改变。
I have a view. It is part of a large and complex project. When the frame of my view is set, layoutSubviews is not automatically called.
Playing around with this, I tried putting the following method into my view:
-(void) setFrame: (CGRect) frame {
NSLog (@"setting frame");
[super setFrame: frame];
[self setNeedsLayout];
}
At this point, layout was redone when the frame was set, as expected. And if I commented out the setNeedsLayout line, layout was not done. Furthermore, when I click on "step into" at the line where super is called, the debugger does not step into anything. So as far as I can tell, super's setFrame method is not being overridden.
FWIW, I also did a small test project in which I changed the frame of a view. In this case, layoutSubviews was called when I did that. [setNeedsLayout was not called in my test project.] So something in my large project is changing the behavior. But I don't know what that something is.
Can anyone explain what might be going on?
EDIT: to clarify, my frame change is changing the view's height. The width is not changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论