2011-11-08 8 views
1

iPhoneの向きが横向きに変わったときにViewControllerを押すと、ViewControllerの向きを変更するときに問題が発生します。ViewController向きの変更

Iは、そのコードを使用する:

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
// Do any additional setup after loading the view from its nib. 
Storage *strg = [Storage sharedStorage]; 

if ([strg.orient intValue] == 2) 
{ 
    [[UIApplication sharedApplication] setStatusBarOrientation: 
    UIInterfaceOrientationLandscapeRight]; 

    UIScreen *screen = [UIScreen mainScreen]; 
    CGFloat screenWidth = screen.bounds.size.width; 
    CGFloat screenHeight = screen.bounds.size.height; 
    UIView *navView = [[self navigationController] view]; 
    navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth); 
    navView.transform = CGAffineTransformIdentity; 
    navView.transform = CGAffineTransformMakeRotation(1.57079633); 
    navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0); 
    [UIView commitAnimations]; 
} 
if ([strg.orient intValue] == 1) 
{ 
    [[UIApplication sharedApplication] setStatusBarOrientation: 
    UIInterfaceOrientationLandscapeLeft]; 

    UIScreen *screen = [UIScreen mainScreen]; 
    CGFloat screenWidth = screen.bounds.size.width; 
    CGFloat screenHeight = screen.bounds.size.height; 
    UIView *navView = [[self navigationController] view]; 
    navView.bounds = CGRectMake(0, 0, screenHeight, screenWidth); 
    navView.transform = CGAffineTransformIdentity; 
    navView.transform = CGAffineTransformMakeRotation(4.71238898); 
    navView.center = CGPointMake(screenWidth/2.0, screenHeight/2.0); 
    [UIView commitAnimations]; 
} 
} 

結果が一致しません。ときどき正しい向きになり、時には逆さまになります。

私はLandcapeRightからLandscapeLeftに(そして逆も)うまくいきますが、問題はポートレートモードになったときだけです。

私が間違っていることは何ですか?

答えて

1

デバイスの向きの変更に本当に反応している場合は、おそらくsetStatusBarOrientationを使用すべきではありません。 shouldAutorotateToInterfaceOrientationとdeviceDidRotateSelectorの通知を使用して、サポートされている向きにビューコントローラを回転させる方が良いと思います。

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

-(void)deviceDidRotateSelector:(NSNotification*) notification { 
// respond to rotation here 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
//Return YES for supported orientations 
return YES; 
} 
+0

これを試しました。動作しません。理由を知らないと、オリエンテーションの変更とナビゲーションを組み合わせると、多くの人がこの問題を抱えていることがわかりました。 – urilevy2

+0

うーん、これは私にとってはうまくいく。私が持っている傾向がある唯一の問題は、私がUIDeviceOrientationUnknownと他の方向に応答していることを確認することです。しかし、私はペン先を使わない。私はすべてをコードしているので、それは問題がないのかもしれません。私はまだsetStatusBarOrientationの使用を避けるべきだと思います。これはデバイスの向きを実際に変更しないためです。デバイスの向きを教えても、ステータスバーの向きを他の何か。 – ader

+0

私はこれを前に試したときに逃したものは分かりませんが、今は動作します!ありがとう!! – urilevy2

関連する問題