如何在我的应用程序中进行定位?
我的应用程序当前处理方向的方式是重新定位视图中的所有项目,其布局可以随着用户与其交互而改变。我遇到了很多问题,例如视图未出现、视图在屏幕旋转之前发生变化等。我想知道处理方向的最佳方法?
The way my app currently deals with orientation is it repositions all of the items in the view, the layout of which can change as the user interacts with it. I've had numerous problems, such as view's not appearing, views changing before the screen rotates etc. I'm wondering the best way to deal with orientation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果横向布局与纵向布局完全不同,我只需在 UIView 子类的 init 方法中加载所有子视图,并将它们添加为子视图。
整个魔法是在
layoutSubviews
方法中完成的,我只检查当时我所处的方向。切勿在layoutSubviews
中调用alloc
、addSubview
、removeFromSuperview
、... 方法。layoutSubviews
应该只包含设置子视图的frame
属性的代码。参考您的问题:
视图未出现:可能忘记了
addSubview
-在屏幕旋转之前调用视图发生变化:您可能在
layoutSubviews
方法之外更新了子视图的一些框架属性If the landscape-layout is completely different from the portrait-layout I just load all subviews in the init-method of my
UIView
-subclass and added them as subviews.The whole magic is done in the
layoutSubviews
-method where I only check in which orientation I am at that moment. Never callalloc
,addSubview
,removeFromSuperview
, ... methods inlayoutSubviews
. ThelayoutSubviews
should only contain code that sets theframe
-properties of subviews.Referring to your problems:
view not appearing: maybe forgot an
addSubview
-callviews changing before the screen rotates: you probably update some frame-properties of subviews outside the
layoutSubviews
-method一种可能性 - 如果您的应用程序仅适用于一种方向,则禁止方向更改。这是一个合理的回应,有些应用程序只能在一个视图中使用。
One possibility - if your app only works with one orientation, disallow orientation changes. This is a reasonable response, some apps are only usable in one view.