UIWindow 的 UIView 子视图不旋转

发布于 2024-12-24 19:49:54 字数 381 浏览 1 评论 0原文

我正在将一个 UIView 添加到一个不是 keyWindow 的 UIWindow 中。我希望 UIView 在设备旋转时旋转。 我需要设置窗口或视图上的任何特殊属性吗?当前视图不旋转。

我知道只有应用程序的 keyWindow 的第一个子视图被告知有关设备旋转的信息。作为测试,我将视图添加到 keyWindow 的第一个子视图中。这会导致视图旋转。然而,由于各种美观原因,作为 keyWindow 第一个子视图的子视图的视图将无法工作。

另一种方法是观察视图中设备方向的变化并自己编写旋转代码。不过,如果可能的话,我想避免编写这段代码(在我看来,有一个额外的窗口更干净)。

I'm adding a UIView to a UIWindow that's not the keyWindow. I'd like the UIView to rotate when the device rotates. Any special properties on the window or the view I need to set? Currently the view is not rotating.

I'm aware of the fact that only the first subview of the application's keyWindow is told about device rotations. As a test I added my view to the first subview of the keyWindow. This causes the view to rotate. However the view being a subview of the keyWindow's first subview won't work for various aesthetic reasons.

An alternative approach is observing device orientation changes in the view and writing the rotation code myself. However I'd like to avoid writing this code if possible (having an additional window is cleaner in my opinion).

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

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

发布评论

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

评论(2

落叶缤纷 2024-12-31 19:49:54

UIView 不处理旋转,UIViewController 处理。
因此,您所需要做的就是创建一个 UIViewController,它实现 shouldAutorotateToInterfaceOrientation 并将此控制器设置为 UIWindow 的 rootViewController

类似的东西:

    UIViewController * vc = [[[MyViewController alloc] init] autorelease];

    vc.view.frame = [[UIScreen mainScreen] bounds];
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

    //you vc.view initialization here

    UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.windowLevel = UIWindowLevelStatusBar;
    window.backgroundColor = [UIColor clearColor];
    [window setRootViewController:vc];
    [window makeKeyAndVisible];

我使用了这个 MyViewController,因为我希望它反映主应用程序的更改

    @interface MyViewController : UIViewController

    @end

    @implementation MyViewController

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        UIWindow *window = ((UIWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]);
        UIViewController *controller = window.rootViewController;

        if (!controller) {
            NSLog(@"%@", @"I would like to get rootViewController of main window");
            return YES;
        }
        return [controller shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    @end

,但您始终可以返回如果您愿意,可以选择任何方向或写下您的逻辑。

UIView doesn't handle rotations, UIViewController does.
So, all you need is to create a UIViewController, which implements shouldAutorotateToInterfaceOrientation and sets this controller as a rootViewController to your UIWindow

Something like that:

    UIViewController * vc = [[[MyViewController alloc] init] autorelease];

    vc.view.frame = [[UIScreen mainScreen] bounds];
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

    //you vc.view initialization here

    UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.windowLevel = UIWindowLevelStatusBar;
    window.backgroundColor = [UIColor clearColor];
    [window setRootViewController:vc];
    [window makeKeyAndVisible];

and I used this MyViewController, cause I want it to reflect changes of main application

    @interface MyViewController : UIViewController

    @end

    @implementation MyViewController

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        UIWindow *window = ((UIWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]);
        UIViewController *controller = window.rootViewController;

        if (!controller) {
            NSLog(@"%@", @"I would like to get rootViewController of main window");
            return YES;
        }
        return [controller shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    @end

but you can always just return YES for any orientation or write your logic, if you wish.

疑心病 2024-12-31 19:49:54

您可以将以下代码添加到您的项目中

[[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(didRotate:)
名称:UIDeviceOrientationDidChangeNotification 对象:nil];

并创建了处理它的函数。

You can add below code to your project

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

and created the function to handle it.

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