UIView 子视图不会在方向改变时自动调整大小

发布于 2024-09-16 08:16:26 字数 379 浏览 6 评论 0原文

在我的 iPhone 应用程序中,我有一个具有两个视图(本质上是前视图和后视图)的视图控制器。前视图是主 UIView,后视图是辅助 UIView,在显示背面时使用 [self.view addSubview:backView] 添加为子视图,并使用 [backView removeFromSuperview] 隐藏它时。但是,当方向改变时,我遇到以下问题:主 UIView (frontView) 旋转并旋转。它的所有元素都正确调整大小,但辅助/子视图 UIView (backView) 不会旋转和旋转。它的所有元素都无法正确调整大小。 有人对如何根据我在 Interface Builder 中设置的规则使辅助 UIView 自动调整大小有建议吗?

In my iPhone app, I have a view controller with two views (essentially, a front & back view). The front view is the main UIView, and the back view is a secondary UIView which is added as a subview using [self.view addSubview:backView] when showing the back and [backView removeFromSuperview] when hiding it. However, when the orientation changes, I have the following issue: the main UIView (frontView) rotates & all of its elements resize properly, but the secondary/subview UIView (backView) does not rotate & all of its elements do not resize properly. Does anyone have suggestions on how to make the secondary UIView autoresize properly according to the rules I have set in Interface Builder?

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

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

发布评论

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

评论(5

許願樹丅啲祈禱 2024-09-23 08:16:26

最后,我找到的解决方案只是将我的 UIView 分离到单独的 UIViewController 中,并确保我希望能够旋转的任何视图只有一个 UIView。

In the end, the solution I found was simply to separate my UIViews into separate UIViewControllers, and make sure that any views that I wanted to be able to rotate only had one UIView.

拥抱影子 2024-09-23 08:16:26

如果我理解正确的话,在旋转时“backView”已从它的超级视图中删除,是吗?如果是这样,那就是问题的原因。 autoresize 属性确定视图相对于其父视图如何调整大小。如果它没有超级视图,它将不会调整大小。

也许使用 [backView setHidden:YES] 而不是 [backView removeFromSuperview] 足以满足您的需求。

If I understand correctly, at the time of rotation 'backView' has been removed from it's superview, yeah? If so, that's the cause of the problem. The autoresize property determines how the view resizes relative to it's superview. If it doesn't have a superview it won't resize.

Perhaps using [backView setHidden:YES] instead of [backView removeFromSuperview] will be sufficient for your needs.

感性不性感 2024-09-23 08:16:26

我遇到了同样的问题,这是我根据 imaginaryboy 解决的方法
建议(谢谢!)

在 viewDidLoad 处将 backview 添加到 viewcontroller 并同时隐藏它。需要的时候显示,需要的时候再隐藏。将后视图的大小调整设置为IB中的UIViewAutoresizingFlexibleWidth(或者我猜的代码,我使用了IB)

I had the same problem, here is how I fixed it based on imaginaryboy's
suggestions (thanks!)

Add the backview to the viewcontroller at viewDidLoad and hide it at the same time. Show it when needed, Hide it again. Set the resizing of the backview to UIViewAutoresizingFlexibleWidth in IB (or code I guess, I used IB)

赏烟花じ飞满天 2024-09-23 08:16:26

并不是说这是同一个问题,而是在我的应用程序中添加 2 个子视图时我发现了类似的问题:didFinishLaunchingWithOptions 方法。由于您上面的参考使用的是 [self.view addSubview:view],我理解这意味着 self 不是您的 UIWindow。将附加视图控制器添加到应用程序委托窗口 (UIWindow) 时,第二个视图控制器将不会接收任何旋转事件,并且永远不会旋转。只有添加到 UIWindow 的第一个视图控制器才会旋转。请参阅:技术问答 QA1688 我相信这也会影响在第一个视图之后添加的视图,其中第一个视图稍后从超级视图中删除。

我最终遵循了我在其他地方读到的建议,为每个方向使用单独的视图,从而消除了担心调整大小行为的需要。一如既往,YMMV。

Not that this is the same problem, but I found a similar problem when adding 2 subviews in my application:didFinishLaunchingWithOptions method. Since your reference above is using [self.view addSubview:view], I would understand that to mean that self is not your UIWindow. When adding an additional view controller to your App Delegate window (UIWindow), the second view controller will NOT receive any rotation events and will never rotate. Only the first view controller added to UIWindow will rotate. See:Technical Q&A QA1688 I believe this also affects views added after the first view where the first view is later removed from the superview.

I ended up following the suggestion I read elsewhere to use separate views for each orientation, thereby eliminating the need to worry about resizing behavior. As always, YMMV.

°如果伤别离去 2024-09-23 08:16:26

或者;如果你想避免额外的控制器,你可以通过在 willRotateToInterfaceOrientation:: 中设置 view.frame 来达到相同的效果,如下所示

    if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ;//set stubborn view.frame for landscape orientation

    else ; //set stubborn view.frame for portrait orientation

。这很简单。

Or; if you want to avoid an additional controller, you can achieve the same effect by setting view.frame in willRotateToInterfaceOrientation:: like so

    if(UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) ;//set stubborn view.frame for landscape orientation

    else ; //set stubborn view.frame for portrait orientation

Although it feels like a hack; it's simple.

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