2013-02-21 30 views
13

Ứng dụng của tôi có thể tự động chia sẻ nhưng tôi cần một trong các chế độ xem chỉ hiển thị ở chế độ dọc và không biết cách đạt được điều này.Ngăn chặn autorotate cho một bộ điều khiển xem?

Tôi cố gắng này (trong số những thứ khác) nhưng quan điểm trong câu hỏi vẫn xoay:

// ViewController.m 

-(BOOL)shouldAutorotate 
{    
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait; 
} 

Có thể ai đó vui lòng chỉ ra những gì tôi đang làm sai? Cảm ơn.

-edit-

Đó là dành cho iOS 6.1

+0

Phiên bản SDK nào bạn đang phát triển? –

+0

http://stackoverflow.com/questions/12630359/ios-6-how-do-i-restrict-some-views-to-portrait-and-allow-others-to-rotate – msk

+0

iOS 6, xin lỗi. Tôi đã cập nhật OP. – Robert

Trả lời

30

Khi một UINavigationController là tham gia, tạo ra một thể loại trên UINavigationControlleroverride supportedInterfaceOrientations.

#import "UINavigationController+Orientation.h" 

@implementation UINavigationController (Orientation) 

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

-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

@end 

Hiện tại, các vùng chứa iOS (chẳng hạn như UINavigationController) không hỏi con cái của họ để xác định xem chúng có nên tự động hay không.

Làm thế nào để tạo ra một loại
1. Thêm một tập tin mới (loại c- Mục tiêu dưới touch cacao)
2. Category: Định hướng trên UINavigationController
3. Thêm mã ở trên để UINavigationController+Orientation.m

+0

Vâng, có một 'UINavigationController' liên quan nhưng tôi phải thú nhận để không biết chính xác những gì bạn có nghĩa là bởi subclassing nhưng tôi sẽ đi làm một số đọc. – Robert

+0

@Robert Chỉ cần thêm một danh mục trên 'UINavigationController' xem chỉnh sửa của tôi –

+0

Điều này cũng áp dụng cho một phụ huynh UITabBarController – Halpo

2

Swift 3 phiên bản câu trả lời được chấp nhận:

extension UINavigationController { 

    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask { 
     // Change `.portrait` to whatever your default is throughout your app 
     return topViewController?.supportedInterfaceOrientations ?? .portrait 
    } 

    open override var shouldAutorotate: Bool { 
     return true 
    } 
}