方向更改后奇怪的 UIView 大小



我根据主视图的大小制作日志,我在日志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

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

因此,当设备是 UIInterfaceOrientationMaskPortrait上下颠倒时,视图不应旋转。

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

要解决此问题,请执行以下操作:

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

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

最新更新