2013-11-03 25 views
1

我从主视图的大小制作日志,我在日志上的orientation=4上遇到了奇怪的大小。我无法弄清楚问题所在。这里是我的代码:方向改变后奇怪的UIView大小

- (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 

回答

0

你是不是支持方向UIInterfaceOrientationMaskPortraitUpsideDown (即方向4)。

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

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

要解决这个问题:

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

并确保你已经倒挂选择,以及对我们的支持设备的方向。

+0

谢谢,但我不想支持UpsideDown方向。额外的UIInterfaceOrientationMaskPortraitUpsideDown没有解决问题 – user2948919

+0

你的问题是,你无法弄清楚是什么使奇怪的大小。这是因为它不支持方向4,因此即使您尝试旋转,数值仍然是以前的方向。在模拟器上可能不那么明显。但在设备上,您可以尝试将设备颠倒过来,但不会调用UI旋转。 实质上,当移动到方向4时不会更新UI,因为它不受支持。 – TreeTree

+0

亲爱TreeTree,我不知道我被误解了,但我加了UIInterfaceOrientationMaskPortraitUpsideDown的supportedInterfaceOrientations方法,检查了所有的关于“支持的界面面向”选项“项目摘要”下,它的工作是一样的。 – user2948919