禁用单个 UIView 的自动旋转 [英] Disabling autorotate for a single UIView

查看:16
本文介绍了禁用单个 UIView 的自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这看起来应该相对简单,但我已经用谷歌搜索了一个小时,似乎找不到我需要的东西.

Okay, this seems like it should be relatively simple, but I've been Googling for the better part of an hour, and can't seem to find what I need.

我有一个视图控制器,它有几个不同的部分:背景视图、标题视图和一些按钮.现在,我希望标题和按钮正确自动旋转(当我从 shouldAutorotateToInterfaceOrientation: 返回 YES 时它们会自动旋转:),但在任何情况下背景视图都不应旋转.有没有合适的方法来做到这一点?

I have a view controller that has a few different parts: a background view, a header view, and a few buttons. Now, I want the header and buttons to autorotate properly (they do, when I return YES from shouldAutorotateToInterfaceOrientation:), but under no circumstances should the background view rotate. Is there a proper way to do this?

推荐答案

如果您不希望在将背景更改为正确方向后临时旋转背景视图,则需要关闭自动旋转风景.没有两种办法.

If you don't want temporary rotations of the background view after which the background is changed to have the correct orientation, you'll need to turn off Autorotation for the View. No two ways about it.

您需要自己处理按钮的旋转.你可以做一个漂亮的布局,或者把它们放在一个滚动视图上,然后调整它的大小.

You would need to handle the rotation of the buttons yourself. You can either make a nice layout or put them on for example a Scrollview and just resize that.

无论哪种方式,您都需要添加一个观察者来自己旋转它,

Either way, you'd need to add an observer to rotate it yourself,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

在 didRotate 中,您可以调整大小,如果您愿意,可以使用漂亮的动画过渡.

And in didRotate you do the resize, with a nice animated transition, if you like.

- (void) didRotate:(NSNotification *)notification {
 int ori=1;
    UIDeviceOrientation currOri = [[UIDevice currentDevice] orientation];
    if ((currOri == UIDeviceOrientationLandscapeLeft) || (currOri == UIDeviceOrientationLandscapeRight)) ori=0;
}

希望对您有所帮助.如果导航栏或状态栏导致视图隐藏在顶部栏下方,则有多种方法可以解决该问题.

Hope that sorts you. If Navigation bars or status bars cause the View to get tucked in under the top bar, there are ways to fix that.

这篇关于禁用单个 UIView 的自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆