当设备方向发生变化时,导致子视图重绘的正确方法是什么?

发布于 2024-10-09 12:17:55 字数 172 浏览 0 评论 0原文

我有一个带有一个视图和一个子视图的视图控制器。当设备方向更改时,视图会自动重绘,但子视图不会。如果我明确执行 [subview setNeedsDisplay] 它会重绘,但我没有一个逻辑位置可以将该调用放入我的代码中。而且,由于我不必明确告诉视图重绘,所以告诉子视图重绘似乎是违反直觉的。有没有办法让子视图在视图重绘时自动重绘?

I have a view controller with a view and one subview. When the device orientation changes the view auto-redraws but the subview doesn't. If I explicitly do [subview setNeedsDisplay] it redraws but I don't have a logical place to put that call in my code. And, since I don't have to explicitly tell the view to redraw, it seems counter-intuitive to tell the subview to redraw. Is there a way to make the subview auto-redraw when the view redraws?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-10-16 12:17:55

我发现以下方法有效。如果这不是最好的方法,请有人提供更好的方法。

subview.m 文件的 initWithFrame: 方法中包含以下内容

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.contentMode = UIViewContentModeRedraw;

尽管这可以工作并且非常适合我的代码,但我不明白为什么子视图需要这些属性已设置,但视图未设置。

I found that the following works. If it isn't the best approach somebody please give a better way to do it.

Include the following in the subview.m file, in the initWithFrame: method

self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.contentMode = UIViewContentModeRedraw;

Even though this works and fits nicely into my code, I don't see why the subview needs these properties set but the view doesn't.

多彩岁月 2024-10-16 12:17:55

Swift 3/iOS10 语法:

    self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.contentMode = .redraw

Swift 3/iOS10 syntax:

    self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.contentMode = .redraw
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文