2012-01-10 92 views
1

我創建了一個支持所有方向的應用程序。當在橫向模式下啓動應用程序時,UIOrientation不起作用

另外我在viewDidLoad方法創建的表和搜索欄(不使用XIB)。我還寫了下面的代碼來調整表格視圖和搜索欄的位置。

它可以正常工作,當我在縱向視圖啓動我的應用程序,當我旋轉到橫向視圖,它完美地調整意見。

但是,當我啓動我在橫向模式下的應用程序,視圖不作調整,相反,他們採取縱向視圖的定位。但再次當我旋轉肖像,然後風景它工作正常。

也保證了正確的方向被捕獲後,我寫了NSLog這個方法裏面,它讓我看到正確的值。

爲了確保明確調用此方法(adjustViewsForOrientation),我也調用了viewDidLoad中的方法。

[self adjustViewsForOrientation:self.interfaceOrientation]; 

下面是代碼片段的休息:

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation { 
    if (orientation == UIInterfaceOrientationLandscapeLeft || 
      orientation == UIInterfaceOrientationLandscapeRight) { 
     aTableView.frame = CGRectMake(705, 220, 320, 280); 
     sBar.frame=CGRectMake(805, 0, 220, 40); 
     NSLog(@"inside landscape");   
    } 
    else if (orientation == UIInterfaceOrientationPortrait || 
       orientation == UIInterfaceOrientationPortraitUpsideDown) { 
     aTableView.frame = CGRectMake(450, 220, 320, 280); 
     sBar.frame=CGRectMake(550,3,220,40); 
    } 
} 

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
           duration:(NSTimeInterval)duration { 
    // [self adjustViewsForOrientation] 
    [self adjustViewsForOrientation:toInterfaceOrientation]; 
    NSLog(@"landscap"); 
} 
+0

可以打印出的NSLog(@ 「來定位:%d」,toInterfaceOrientation);並向我們​​展示結果? – 2012-01-10 15:08:17

+0

A的副本 - http://stackoverflow.com/questions/8775911/landscapeorientation-on-start-of-didload-method-in-objective-c/8775924#8775924 – rishi 2012-01-10 16:02:28

回答

0

試試這個辦法,它可以幫助。在viewDidLoad方法註冊爲UIDeviceOrientationDidChange:

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

,改變-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation

-(void) adjustViewsForOrientation:(NSNotification *)notification

和通過獲取取向值

例如獲得新的定向UIDeviceOrientation newDeviceOrientation = [notification orientation];

相關問題