2011-11-11 2 views
3

私は画面の向きを変更しているアプリケーションがあります。しかし、最初の画面の向きを変更して次の画面に進むと、その変更は維持されません。オリエンテーションが変更されたときにアプリケーションを管理する方法は?

添付の画像は、よりわかりやすくするためのものです。

first screen of view in portrait mode

first screen of view in landscape mode

second screen of view in landscape modesecond screen of view in portrait mode


主な問題は、デバイスが回転するときに最初の画面が回転するが、第2の新しい向きで起動しないことです。
画面を起動した後に方向を変更したときにのみ回転します。

どうすれば修正できますか?

答えて

6

問題は、デバイスを回転させるときに

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

メソッドが呼び出されるということですが、あなたは次の画面に行くとき、このメソッドは呼び出されません。デバイスを再び回転させると、そのメソッドが呼び出されます。

したがって、デバイスの現在の向きとして次の画面の向きを変更する場合は、viewDidLoadメソッドまたはViewWillApper()メソッドでデバイスの現在の向きを確認します。それに応じて、現在のビューを設定します。

+0

おかげブロック・アダムズをコーディングハッピー... –

+0

は、それはiOS6以降廃止されました。 –

0

このメソッドを呼び出します。

-(void) viewWillAppear:(BOOL)animated{ 
    UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 

    [self willAnimateRotationToInterfaceOrientation:orientation duration:0]; 

} 

-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 


    enter code here 
    set frame......... 
    } 
    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 

     set frame......... 

    } 


} 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Overriden to allow any orientation. 
    return YES; 
} 
+0

あなたの提案に感謝しますが、私は最初の画面に2番目の画面に行くとき、私は横に右に回転し、次に右側に余白が残っている場合。どのように画面からそのギャップを削除する? – ios

+0

画像ビューのフレームとその他のすべてのコントロールを設定します。 –

1
Use this buddy... 

If suppose, you are adding UIImageView to your self.view as 

**in .h**, 

UIImageView *topView 

**in .m**, 

    topView = [[UIImageView alloc] initWithImage: 
       [UIImage imageNamed:@“anyImage.png"]]; 
    topView.userInteractionEnabled = YES; 

    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     topView.frame = // set your dimensions as per landscape view 
    } 
else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     topView.frame = // set your dimensions as per portrait view 

} 


    [self.view addSubView : topView]; 

**Add this lines at end of your viewDidLoad.** 

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



**And the method** 

    - (void)didRotate:(NSNotification *)notification 
    { 
    UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

    switch (orientation) 
    { 
     case UIInterfaceOrientationLandscapeLeft: 
     { 
      topView.frame = // set your dimensions as per landscape view 
      break; 
     } 
     case UIInterfaceOrientationLandscapeRight: 
     { 
      topView.frame = // set your dimensions as per landscape view 
      break; 
     } 
     case UIInterfaceOrientationPortraitUpsideDown: 
     { 
      topView.frame = // set your dimensions as per portrait view 
      break; 
     } 
     case UIInterfaceOrientationPortrait: 
     { 
      topView.frame = // set your dimensions as per portrait view 
      break; 
     }   
    } 
} 


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

..

関連する問題