UIView 在将方向切换为横向后更改宽度?
我有一个带有 2 个子视图的 UIView。 UIView 设置为 UIViewAutoresizingFlexibleWidth。现在,当它进入layoutSubviews时,它会调整子视图相对于UIView的框架大小,如下所示:
rect.size.width = self.frame.size.width - rect.origin.x - textLabel.frame.origin.x;
但是在横向模式下会出现问题,因为在纵向模式下,self.frame.size.width(以及与此相关的边界)在开始时响应768,并且788 完成旋转后。这意味着当它旋转时,它似乎会增加 20 个像素,我认为这是状态栏。
当我硬编码 768 时,一切都按预期工作。但我当然不想要这样。我可以将初始状态存储到 var 中并使用它,但我认为这不是正确的方法。
有人有解决办法吗?
I got a UIView with 2 subviews. The UIView is set to UIViewAutoresizingFlexibleWidth. Now when it enters layoutSubviews it resizes the frames of the subviews relative to the UIView like so:
rect.size.width = self.frame.size.width - rect.origin.x - textLabel.frame.origin.x;
But a problem occurs in landscape mode cause in portrait mode the self.frame.size.width (and bounds for that matter) respond 768 at start and 788 after it's done rotating. Meaning when it rotates it seems to add 20pixels which I assume is the status bar.
When I hard code 768 it all works as expected. But I don't want that of course. I could store the initial state into a var and use that but I don't think that's the right way to do it.
Anyone have a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您好,
在我的应用程序中我也遇到了同样的问题。为了解决这个问题,我简单地使用了
didRotateFromInterfaceOrientation:
方法在设备旋转时调整视图的大小。self.view.frame = CGRectMake(0,0,self. view.bounds.size.width,self.view.bounds.size.height);
希望这会有所帮助。
Greetings,
in my application I have been facing the same Problem. To solve it I simply used the
didRotateFromInterfaceOrientation:
method to resize the View when the device is rotated.self.view.frame = CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height);
Hope this will help.