方向改变后的奇怪UIView尺寸

时间:2013-11-03 01:00:15

标签: ios iphone objective-c uiview orientation

我从主视图的大小做出日志,并且我在日志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

1 个答案:

答案 0 :(得分:0)

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

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

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

解决此问题:

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

并确保您已选择颠倒以支持设备方向。