2012-09-24 37 views
14

Tôi đang sử dụng mã nàypresentViewController không hỗ trợ định hướng trong iOS 6

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) 
    { 
     [self presentViewController:navigationControllerCustom animated:YES completion:nil]; 
    } 
    else 
    { 
     [self presentModalViewController:navigationControllerCustom animated:YES]; 
    } 

Ứng dụng của tôi có hai định hướng Portrait và Portrait lộn ngược. Mã này hoạt động tốt với iOS 5.1, nhưng định hướng không hoạt động trên iOS 6

Tôi cũng đã thêm mã này vào lớp navigationControllerCustom tôi

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown); 
} 

Xin hãy giúp tôi để giải quyết vấn đề này.

Xin cảm ơn trước.

+2

ive gặp vấn đề tương tự trong các ứng dụng tôi đã biên soạn cho iOS 6; tất cả các định hướng hoạt động ngoại trừ UIInterfaceOrientationPortraitUpsideDown; kỳ lạ của nó. – MrJre

+0

@MrJre Bất kỳ giải pháp nào giống như thế này thực sự là lạ. –

Trả lời

25

Bạn phải bao gồm này trong bạn đại biểu ứng dụng:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

Ngoài ra hãy chắc chắn rằng View Controller của cả hai đều có những điều sau đây, hoạt động tốt đối với tôi.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown); 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

Các tài liệu cũng nói rằng UINavigationController's không truy vấn đầu View Controller của nó đối với định hướng hỗ trợ, mặc dù Apple kỹ sư qua trên Diễn đàn Nhà phát triển đã nói như vậy ... có vẻ như nó không. Vì vậy, bạn nên thêm một hạng mục cho UINavigationController, đây là một trong tôi sử dụng:

#import "UINavigationController+autorotate.h" 

@implementation UINavigationController (autorotate) 

- (NSUInteger)supportedInterfaceOrientations{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

@end 

Để biết thêm thông tin như thế nào dừng quay hoạt động trên iOS 6 check out this answer

+0

Câu trả lời hoàn hảo, bạn đã giải quyết được sự cố của mình, Cảm ơn :-) –

+1

Danh mục UINavigationController là đủ. – Gujamin

+0

+1 Phần ủy nhiệm ứng dụng rất hữu ích. –

2

Bạn quên:

- (BOOL)shouldAutorotate { 
    return YES; 
} 
+0

Cảm ơn bạn đã trả lời. Tôi đã bổ sung, nhưng nó không hoạt động, bất kỳ thứ gì khác tôi đang thiếu. –

+0

Trong Info.plist của bạn dưới định hướng giao diện được hỗ trợ, bạn có hiển thị dọc và dọc theo chiều dọc không? – rocky

+0

Vâng, tôi cũng đã làm điều đó, bất kỳ điều gì khác? –

1

Chỉ cần sao chép dán đoạn trên mã của bạn

@implementation UINavigationController (rotation) 
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers. 
- (NSUInteger)supportedInterfaceOrientations { 
return [self.topViewController supportedInterfaceOrientations]; 
} 

@end 
1

Nếu bạn định bật hoặc tắt xoay vòng cho tất cả trình điều khiển chế độ xem mà bạn không cần phải phân lớp UINavigationController. Thay vào đó, hãy sử dụng:

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

trong AppDelegate của bạn.

Nếu bạn có kế hoạch để hỗ trợ tất cả các định hướng trong ứng dụng của bạn, nhưng định hướng khác nhau trên PHỤ HUYNH Xem Controllers (UINavigationController ngăn xếp chẳng hạn) bạn nên sử dụng

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

kết hợp với các phương pháp sau đây trong Chánh của bạn Xem Controller.

- (BOOL)shouldAutorotate 

- (NSUInteger)supportedInterfaceOrientations 

Nhưng nếu bạn có kế hoạch để có các cài đặt định hướng khác nhau trong TRẺ EM ViewControllers khác nhau trong điều hướng cùng một ngăn xếp (như tôi), bạn cần phải kiểm tra các ViewController hiện trong điều hướng stack.

tôi đã tạo ra những điều sau đây trong UINavigationController lớp con của tôi:

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    int interfaceOrientation = 0; 

    if (self.viewControllers.count > 0) 
    { 
     id viewController; 
     DLog(@"%@", self.viewControllers); 
     for (viewController in self.viewControllers) 
     { 
      if ([viewController isKindOfClass:([InitialUseViewController class])]) 
      { 
       interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
      } 
      else if ([viewController isKindOfClass:([MainViewController class])]) 
      { 
       interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
      } 
      else 
      { 
       interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown; 
      } 
     } 
    } 
    return interfaceOrientation; 
} 

Vì bạn không thể kiểm soát được nữa từ trẻ em ViewControllers các thiết lập chuyển động quay của bộ điều khiển xem giới thiệu bạn phải bằng cách nào đó chặn những gì điều khiển xem hiện đang trong menu ngăn xếp . Vì vậy, đó là những gì tôi đã làm :). Hy vọng rằng sẽ giúp!

+0

Cảm ơn @codeFi, đề xuất của bạn đã khắc phục được sự cố của tôi. Tôi đã phải vật lộn khá nhiều khi di chuyển từ iOS5 sang iOS6 với các vấn đề định hướng thiết bị khác nhau. Bạn làm cho ngày của tôi! –

1

Subclass UINavigationController và ghi đè lên shouldAutorotate và supportedInterfaceOrientations như thế này:

-(BOOL)shouldAutorotate 
{ 
    return self.topViewController.shouldAutorotate; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return self.topViewController.supportedInterfaceOrientations; 
} 

Và bây giờ bạn có thể kiểm soát định hướng của bạn là mỗi ViewController. Chỉ cần ghi đè lên hai phương thức trong mỗi ViewController.

Các vấn đề liên quan