2011-11-08 121 views
1

當iPhone將方向更改爲橫向時,我正在推送ViewController,並且無法更改ViewController的方向。ViewController方向更改

我使用的代碼:

- (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 strait(反之亦然)時,它工作正常,問題只在我進入肖像模式時出現。

任何想法我做錯了什麼?

回答

1

如果你真的在響應設備方向改變,他們可能不應該使用setStatusBarOrientation。我認爲你最好讓viewcontroller使用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