【问题标题】:Strange UIView sizes after orientation has been changed方向改变后奇怪的 UIView 大小
【发布时间】:2013-11-03 01:00:14
【问题描述】:

我根据主视图的大小制作了一个日志,我在日志上的orientation=4 遇到了奇怪的大小。我无法弄清楚问题是什么。这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

- (void)orientationDidChange:(NSNotification *)note
{
    int orientation = [[UIDevice currentDevice] orientation];
    NSLog(@"orientation=%i -------------------------------",orientation);
    CGRect f = self.view.frame;                     NSLog(@"self.view.frame =(%f, %f, %f, %f)",f.origin.x, f.origin.y, f.size.width, f.size.height);
    f = self.view.bounds;                           NSLog(@"self.view.bounds=(%f, %f, %f, %f)",f.origin.x, f.origin.y, f.size.width, f.size.height);
    f = [[UIScreen mainScreen] applicationFrame];   NSLog(@"main.app.frame  =(%f, %f, %f, %f)",f.origin.x, f.origin.y, f.size.width, f.size.height);
    f = [[UIScreen mainScreen] bounds];             NSLog(@"main.bounds     =(%f, %f, %f, %f)",f.origin.x, f.origin.y, f.size.width, f.size.height);

}

结果如下:

orientation=1 -------------------------------
self.view.frame =(0.000000, 20.000000, 320.000000, 460.000000)
self.view.bounds=(0.000000, 0.000000, 320.000000, 460.000000)
main.app.frame  =(0.000000, 20.000000, 320.000000, 460.000000)
main.bounds     =(0.000000, 0.000000, 320.000000, 480.000000)

orientation=0 -------------------------------
self.view.frame =(0.000000, 20.000000, 320.000000, 460.000000)
self.view.bounds=(0.000000, 0.000000, 320.000000, 460.000000)
main.app.frame  =(0.000000, 20.000000, 320.000000, 460.000000)
main.bounds     =(0.000000, 0.000000, 320.000000, 480.000000)

orientation=4 -------------------------------
self.view.frame =(0.000000, 20.000000, 320.000000, 460.000000) <== First time size
self.view.bounds=(0.000000, 0.000000, 320.000000, 460.000000) <== First time size
main.app.frame  =(0.000000, 20.000000, 320.000000, 460.000000) <== First time size
main.bounds     =(0.000000, 0.000000, 320.000000, 480.000000) <== First time size

orientation=2 -------------------------------
self.view.frame =(20.000000, 0.000000, 300.000000, 480.000000)
self.view.bounds=(0.000000, 0.000000, 480.000000, 300.000000)
main.app.frame  =(20.000000, 0.000000, 300.000000, 480.000000)
main.bounds     =(0.000000, 0.000000, 320.000000, 480.000000)

orientation=4 -------------------------------
self.view.frame =(20.000000, 0.000000, 300.000000, 480.000000) <== Why is it different?
self.view.bounds=(0.000000, 0.000000, 480.000000, 300.000000) <== Why is it different?
main.app.frame  =(20.000000, 0.000000, 300.000000, 480.000000) <== Why is it different?
main.bounds     =(0.000000, 0.000000, 320.000000, 480.000000) <== Why is it different?

我为解决此问题所采取的步骤如下:

- cmd+right arrow
- cmd+right arrow
- cmd+left arrow

【问题讨论】:

    标签: ios iphone objective-c uiview orientation


    【解决方案1】:

    您不支持方向 UIInterfaceOrientationMaskPortraitUpsideDown(即方向 4)。

    因此,当设备为 UIInterfaceOrientationMaskPortraitUpsideDown 时,视图不应旋转。

    请注意,当您旋转到方向 4 时,这些值与前一个方向的尺寸完全相同。

    解决这个问题:

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortraitUpsideDown;
    }
    

    并确保您已为支持设备方向选择了倒置。

    【讨论】:

    • 谢谢,但我不想支持 UpsideDown 方向。附加的 UIInterfaceOrientationMaskPortraitUpsideDown 并不能解决问题
    • 您的问题是您无法弄清楚是什么造成了奇怪的尺寸。这是因为它不支持方向 4,因此即使您尝试旋转,值仍然是之前的方向。在模拟器上可能不太明显。但在设备上,您可以尝试将设备倒置,但不会调用 UI 旋转。本质上,移动到方向 4 时不会更新任何 UI,因为它不受支持。
    • 亲爱的TreeTree,我不知道我被误解了什么,但我为supportedInterfaceOrientations方法添加了UIInterfaceOrientationMaskPortraitUpsideDown,检查了“项目摘要”下“支持的界面方向”的所有选项,它的工作方式相同。
    • 它似乎总是继承以前的方向值。
    • 你能在 UIView 中添加一些东西吗,例如一个按钮。并查看按钮是否也在 UpsideDown 旋转。