“刷新”查看设备旋转
我在 viewDidLoad
中设置了视图。所有不同的框架和子视图等都是相对于 self.view 定义的。因此,无论 self.view
的大小如何,子视图总是会缩小或扩展以适应(就像以前一样)。
因此,当我旋转设备时,我希望视图旋转(使用shouldAutoRotateToInterfaceOrientation:...就足够简单了),但子视图保持相同的形状。
调用 [self viewDidLoad];
使所有元素都适合,但在之前的布局之上放置一个新层(这很明显......但我只是说解释一下我的意思)。
有什么办法可以刷新子视图的框架之类的吗?说实话,我不知道其他人会做什么。我是否必须将所有视图作为属性放入 .h 文件中,并在 didRotate...
上手动执行所有操作?
I have my view set up in viewDidLoad
. All the different frames and such of the subviews have been defined relative to self.view
. Therefore it doesn't matter what size self.view
is the subviews will always shrink or expand to fit (as it were).
So when I rotate my device I want the view to rotate (easy enough with shouldAutoRotateToInterfaceOrientation:...
) but the subviews stay the same shape.
Calling [self viewDidLoad];
makes all the elements fit, but puts a new layer on top of the previous layout (which is obvious... but i'm just saying to explain what I mean).
Is there any way to refresh the frames of the subviews or something? I don't know what other people do to be honest. Am I going to have to put ALL of my views into the .h file as properties and do everything manually on didRotate...
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有三个选择:
如果自动调整大小蒙版足以定位您的视图,请在创建子视图时为每个子视图分配正确的自动调整大小蒙版。
如果自动调整大小蒙版不够,请覆盖
willAnimateRotationToInterfaceOrientation:duration:
并在该方法中重新定位子视图。我将创建一个自定义方法,它将方向作为参数,并负责布局所有子视图。然后,您可以从willAnimateRotationToInterfaceOrientation:duration:
和viewDidLoad
调用此方法。您还可以创建自定义
UIView
子类,并使视图控制器的视图成为此类的实例。然后重写layoutSubviews
以根据视图的大小定位所有子视图。这种方法意味着您的自定义视图管理其子视图而不是视图控制器。You have three options:
If autoresizing masks are good enough to position your views, assign the correct autoresizing mask to each subview when you create them.
If autoresizing masks are not sufficient, override
willAnimateRotationToInterfaceOrientation:duration:
and reposition your subviews in that method. I would create a custom method that takes the orientation as a parameter and is responsible for laying out all subviews. You can then call this method fromwillAnimateRotationToInterfaceOrientation:duration:
and fromviewDidLoad
.You could also create a custom
UIView
subclass and make your view controller's view an instance of this class. Then overridelayoutSubviews
to position all subviews depending on the view's size. This approach implies that your custom view manages its subviews instead of the view controller.