添加 UIButton 来解决窗口旋转问题

发布于 2024-11-09 10:32:59 字数 836 浏览 2 评论 0原文

我正在做:

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    CGPoint point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
    point = CGPointMake(window.frame.size.width - v.frame.size.width - 10, 30);
    point = CGPointMake(point.x + offsetLeft, point.y + offsetTop);
    v.center = point;
    timer1 forMode:NSDefaultRunLoopMode];
    [window addSubview:v];

一个问题是,当我在横向模式下执行:

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

并打印:

NSLog(@"WINDOW WIDTH IS %f AND HEIGHT IS %f", window.frame.size.width, window.frame.size.height);

我得到:

WINDOW WIDTH IS 768.000000 AND HEIGHT IS 1024.000000

这不是真的.. 似乎它不关心方向。

I am doing:

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
    CGPoint point = CGPointMake(window.frame.size.width/2, window.frame.size.height/2);
    point = CGPointMake(window.frame.size.width - v.frame.size.width - 10, 30);
    point = CGPointMake(point.x + offsetLeft, point.y + offsetTop);
    v.center = point;
    timer1 forMode:NSDefaultRunLoopMode];
    [window addSubview:v];

One issue is that when I do in landscape mode:

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];

and I print:

NSLog(@"WINDOW WIDTH IS %f AND HEIGHT IS %f", window.frame.size.width, window.frame.size.height);

I got:

WINDOW WIDTH IS 768.000000 AND HEIGHT IS 1024.000000

Which is not true.. it seems that it doesn't care about orientation.

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

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

发布评论

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

评论(4

满天都是小星星 2024-11-16 10:32:59

您不应将视图插入 UIWindow 中。 UIWindow 本身不旋转。它将变换应用于保存根视图的旋转子视图。如果可能的话,您应该将视图插入到根视图中。否则,您将需要计算自己的变换,这很痛苦。

有关此问题的更多详细信息,请参阅此问题。简而言之,这种事情最好通过使用基于视图的应用程序模板来完成,并使用 rootViewController 来获取根视图并将其插入其中。

请注意,您还应该避免请求 UIApplication 的第一个窗口。不保证这是“主”窗口;第一个窗口是最靠后的窗口(并且存在多个窗口的情况并不罕见)。如果您必须使用 UIWindow,请从您关心的视图 ([view window]) 获取它,或者将其绑定到您的 UIApplicationDelegate厦门国际银行。

You should not insert views into the UIWindow. The UIWindow itself does not rotate. It applies transforms to a rotation subview, which holds the root view. You should be inserting your view into the root view if at all possible. Otherwise, you will need to calculate your own transforms, and that's a pain.

See this question for more details on this issue. The short answer is that this kind of thing is best done by using the View-Based Application template, and making use of the rootViewController to get the root view and insert there.

Note that you should also avoid asking for the first window of the UIApplication. This is not guaranteed to be the "main" window; the first window is the furthest-back window (and it is not unusual for there to be more than one window). If you must use a UIWindow, get it from a view you care about ([view window]), or bind it to your UIApplicationDelegate in the XIB.

空城缀染半城烟沙 2024-11-16 10:32:59

我认为只有窗口的第一个子视图实际上接收旋转事件。试试这个。

[[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] addSubview:SOME_SUBVIEW];

I think only the first subview of your window actually receives rotation events. Try this.

[[[[[UIApplication sharedApplication] keyWindow] subviews] objectAtIndex:0] addSubview:SOME_SUBVIEW];
回首观望 2024-11-16 10:32:59

使用 [[UIScreen mainScreen]bounds] 获取框架。它将返回所有方向的适当框架信息。我遇到了类似的问题。我不知道其中的原因。

Use [[UIScreen mainScreen] bounds] to get the frame. It will return the appropriate frame information in all orientations. I faced the similar problem. And I dont know the reason for it.

内心激荡 2024-11-16 10:32:59

您可以找出您的设备面向哪个方向

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

然后您可以对其进行检查:

if (orientation == UIDeviceOrientationPortrait) {
    // ...
} else {
    // ...
}

You can find out which orientation your device is facing with

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

Then you could perform a check on it:

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