添加 UIButton 来解决窗口旋转问题
我正在做:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不应将视图插入
UIWindow
中。UIWindow
本身不旋转。它将变换应用于保存根视图的旋转子视图。如果可能的话,您应该将视图插入到根视图中。否则,您将需要计算自己的变换,这很痛苦。有关此问题的更多详细信息,请参阅此问题。简而言之,这种事情最好通过使用基于视图的应用程序模板来完成,并使用 rootViewController 来获取根视图并将其插入其中。
请注意,您还应该避免请求
UIApplication
的第一个窗口。不保证这是“主”窗口;第一个窗口是最靠后的窗口(并且存在多个窗口的情况并不罕见)。如果您必须使用UIWindow
,请从您关心的视图 ([view window]
) 获取它,或者将其绑定到您的UIApplicationDelegate
厦门国际银行。You should not insert views into the
UIWindow
. TheUIWindow
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 aUIWindow
, get it from a view you care about ([view window]
), or bind it to yourUIApplicationDelegate
in the XIB.我认为只有窗口的第一个子视图实际上接收旋转事件。试试这个。
I think only the first subview of your window actually receives rotation events. Try this.
使用 [[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.
您可以找出您的设备面向哪个方向
然后您可以对其进行检查:
You can find out which orientation your device is facing with
Then you could perform a check on it: