0

我想在 ios 6 中更改视图的方向。当方向更改时,我想设置视图的框架大小。

我的方向更改示例代码:

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSInteger)supportedInterfaceOrientations
{
    NSInteger mask = 0;

    intOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    if(intOrientation == UIInterfaceOrientationPortrait || intOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        mask |= UIInterfaceOrientationMaskPortrait;
    } else 
        if(intOrientation == UIInterfaceOrientationLandscapeLeft)
        {
            mask |= UIInterfaceOrientationMaskLandscapeLeft;
        }
    }
    NSLog (@"mask %d",mask);
    return mask;
}

// Here now mask value is 2 because my default mode is portrait. 

现在,当我将方向更改为横向时,我想设置帧大小。请指导我如何做到这一点。

好的..我可以通过获取版本并在viewdidLoad中比较它来设置像dis这样的视图的帧大小:

orientation = [[UIDevice currentDevice] orientation];

NSString *reqSysVer = @"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
NSLog (@"ios version %@",currSysVer);
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
{
    if (orientation == UIInterfaceOrientationMaskPortrait) {
        self.view.frame = CGRectMake(0, 0, 1000, 800);


        self.tableView1.frame = CGRectMake(1, 0, 764,510);

    }
    else if (orientation == UIInterfaceOrientationMaskLandscapeLeft)
    {
        self.view.frame = CGRectMake(0, 0, 1300, 370);
        self.tableView1.frame = CGRectMake(0, 0, 1300, 370);
        self.view.backgroundColor = [UIColor blackColor];

    }

}
4

2 回答 2

2

您可以在界面方向更改时收到通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

并添加如下功能:

- (void)didRotate:(NSNotification *)notification {   
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientation...)
    {
        // add some code
    }
}
于 2013-02-05T08:48:58.087 回答
1

我认为最好的解决方案是使用自动布局或约束。但您也可以使用检查设备方向

[UIDevice currentDevice].orientation

或检查大小(例如,对于 iPhone 5 支持)

[UIScreen mainScreen].bounds.size.height 

// 更新:考虑在 iOS 8 上,宽度和高度是横向切换的。所以宽度和高度总是取决于现在的方向。

于 2013-02-05T08:38:22.137 回答