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. 

向きを横向きに変更するときに、フレームサイズを設定します。その方法を教えてください。

ok ..バージョンを取得して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

最善の解決策は、Auto Layout または Constraints を使用することだと思います。ただし、次を使用してデバイスの向きを確認することもできます

[UIDevice currentDevice].orientation

または、次を使用してサイズを確認します (iPhone 5 のサポートなど)。

[UIScreen mainScreen].bounds.size.height 

// 更新: iOS 8 で幅と高さが横向きに切り替えられたことを考慮してください。そのため、現在、幅と高さは常に向きに依存しています。

于 2013-02-05T08:38:22.137 に答える