2011-11-11 63 views
3

我有一個應用程序,我正在改變屏幕方向。但是,當我更改第一個屏幕的方向並轉到下一個屏幕時,更改不會持續。如何在方向更改時管理應用程序?

附加的圖像爲了更清楚的理解。

first screen of view in portrait mode first screen of view in landscape mode

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


的主要問題是設備旋轉時,則第一屏幕旋轉,但第二次沒有出現在新的方向發射。
只有在屏幕啓動後我們改變方向時,它纔會旋轉。

我該如何解決這個問題?

回答

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

感謝您的建議,但是當我進入第一個屏幕到第二個屏幕時,如果我旋轉到右側,然後在右側保留餘量。那麼如何從屏幕上消除這種差距? – ios

+0

因此爲imageview和所有其他控件設置了框架。 –

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; 
} 

編碼快樂..

相關問題