2014-11-21 127 views
0

我試圖保留UITableViewcontroller縱向。因此,我不想旋轉到橫向模式。我在下面添加了方法。但它並沒有幫助,請注意,我使用的是iOS 8:禁用UITableViewcontroller從橫向旋轉(保持縱向)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    if(interfaceOrientation== UIInterfaceOrientationPortrait) 
    { 
     return YES; 
    }else 
    { 
     return NO; 
    } 

} 

注意:我通過調用的UITableView UINavigationController的

UINavigationController *navigationController = [[UINavigationController alloc] 
                initWithRootViewController:svc]; 
    // configure the new view controller explicitly here. 




    [self presentViewController:navigationController animated:YES completion: nil]; 

回答

2

shouldAutorotateToInterfaceOrientation:以來的iOS 6.0已被棄用。您應該使用supportedInterfaceOrientationsshouldAutorotate

這裏是你如何做到這一點:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationPortrait; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 

編輯 - 爲UINavigationController

這是一個可能的方式做到這一點:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    if ([self.visibleViewController isKindOfClass:[UITableViewController class]]) 
     return UIInterfaceOrientationPortrait; 
    else 
     return [super preferredInterfaceOrientationForPresentation]; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    if ([self.visibleViewController isKindOfClass:[UITableViewController class]]) 
     return UIInterfaceOrientationMaskPortrait; 
    else 
     return [super supportedInterfaceOrientations]; 
} 

- (BOOL)shouldAutorotate 
{ 
    if ([self.visibleViewController isKindOfClass:[UITableViewController class]]) 
     return NO; 
    else 
     return [super shouldAutorotate]; 
} 

請注意,你不能強迫設備的方向,所以如果應用程序在橫向,然後你推動表視圖控制器,它仍然是橫向。有很多方法可以解決這個問題:

  • 阻止用戶打開表視圖控制器,通過顯示一個警告,要求他們先旋轉設備。
  • 隱藏表格視圖並顯示帶有消息(或其他指示符)的標籤,以通知用戶旋轉其設備。
  • 處理兩個方向。
+0

...謝謝,但它仍然在旋轉......注意我正在使用故事板,這樣做有什麼區別。 – user836026 2014-11-21 17:31:46

+0

不,不應該這樣做。在這些方法中添加斷點以確保它們被調用。 – 2014-11-21 17:53:40

+1

哦,只是注意到你的'UINavigationController'的更新。當'visibleViewController'是你的表視圖控制器時,你需要子類* that *並覆蓋這些方法以返回'portrait'。 – 2014-11-21 17:56:15

1

shouldAutorotateToInterfaceOrientation:depricated。相反,使用:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

- (BOOL)shouldAutorotate 
{ 
    return NO; 
} 
相關問題