2012-12-14 28 views
7

Tôi có bộ điều khiển chế độ xem chính bên trong UINavigationController. Trong bộ điều khiển chế độ xem tổng thể đó, tôi có một nút đẩy bộ điều khiển xem chi tiết có một UIWebView bên trong nó. Tôi muốn bộ điều khiển xem chi tiết này ở chế độ nằm ngang khi được tải. Quay lại bộ điều khiển chế độ xem chính, nó sẽ quay trở lại chế độ dọc. Tôi đang chạy iOS 6 về điều này.Định hướng cảnh quan lực trên iOS 6 trong mục tiêu-C

Tôi đã thấy các câu hỏi tương tự khác nhưng nó không hoạt động trên đầu của tôi. Tôi đã tạo ra một LandscapeViewController đó là một lớp con của UIViewController nơi tôi đã viết những phương pháp:

#pragma mark - Orientation Methods 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

Đây là mã của tôi khi tôi đẩy bộ điều khiển xem chi tiết:

DetailViewController *detailVC = [[DetailViewController alloc] 
             initWithNibName:@"DetailViewController" 
             bundle:nil]; 

    [self.navigationController pushViewController:detailVC 
             animated:YES]; 

Tôi đang nghĩ về nơi để subclass của tôi LandscapeViewController trên đoạn code trên để làm cho nó hoạt động hoặc trên làm thế nào để phân lớp đúng và đẩy điều khiển xem chi tiết của tôi. Tôi cũng có thể trình bày bộ điều khiển xem chi tiết của mình một cách bình thường nếu không thể cho bộ điều khiển điều hướng đẩy bộ điều khiển xem chi tiết của tôi từ chế độ dọc sang ngang. Tôi đang làm sai ở đâu?

+0

Đây chính là điều tôi nghĩ trong đầu: http://stackoverflow.com/questions/11610819/iphone-forcefully-change-orientation-from-portrait-to-landscape-on-navigation?rq = 1 – jaytrixz

Trả lời

8

Xét: Xem A: Chân dung chỉ - Xem B: Cảnh chỉ

tôi không thể làm điều đó trong bộ điều khiển chuyển hướng. Thay vào đó, những gì tôi đã làm là mở một khung nhìn phương thức từ khung nhìn A để xem B và buộc một bộ điều khiển điều hướng mới vào khung nhìn này.

Tính năng này hoạt động với tôi trong iOS5 +.

Bạn cần phải tạo một thể loại cho bộ điều khiển chuyển hướng như thế này:

UINavigationController + Rotation_IOS6.h

#import <UIKit/UIKit.h> 

@interface UINavigationController (Rotation_IOS6) 

- (BOOL)shouldAutorotate; 
- (NSUInteger)supportedInterfaceOrientations; 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; 

@end 

UINavigationController + Rotation_IOS6.h

#import "UINavigationController+Rotation_IOS6.h" 

@implementation UINavigationController (Rotation_IOS6) 

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

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [self.topViewController preferredInterfaceOrientationForPresentation]; 
} 

@end 

Trong AppDelegate.m add:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ 
    return UIInterfaceOrientationMaskAll; 
} 

Sau đó, trong Xem Một:

- (BOOL)shouldAutorotate { 
    return YES; 
} 

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationPortrait; 
} 

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

cũng trong View A để mở Xem B làm điều này:

ViewB *vc = [[ViewB alloc] initWithNibName:@"ViewB" bundle:nil]; 

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc]; 

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

Và, cuối cùng, trong Xem B

- (BOOL)shouldAutorotate { 
    return YES; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 
0

Họ đã đánh hơi chú chó con trong iOS 6 về vấn đề này. Dưới đây là những gì tôi đã tìm ra cho đến thời điểm này:

Trước hết, Apple đã thêm nút "Định hướng giao diện được hỗ trợ" bằng Xcode 4.5. Điều này tương ứng với thuộc tính "Định hướng giao diện được hỗ trợ" trong _info.plist. Các nút này phải được chuyển sang các lựa chọn chính xác trước khi bất kỳ phần còn lại nào hoạt động. (Nếu các nút có vẻ từ chối chuyển đổi, có khả năng là do info.plist bị khóa bởi CVS hoặc một số quy trình khác.)

Tiếp theo, thuộc tính .window.rootViewController phải được đặt và phải trỏ đến "đáy" xem bộ điều khiển trong ngăn xếp. Nói chung đây sẽ là bộ điều khiển điều hướng hoặc bộ điều khiển tab.

Nếu mong muốn tắt tất cả các lần quay, có thể thực hiện bằng các nút hoặc có thể thực hiện, trong bộ điều khiển xem "dưới cùng", phương thức "shouldAutorotate" và trả về NO. (Nếu phương pháp bị bỏ qua thì mặc định là CÓ.)

Mặc dù đã tắt tính năng tự động ghi nhãn với shouldAutorotate, nếu có một MPMoviePlayerViewController được hiển thị, sẽ tự động sửa. Chỉ bật/tắt các nút định hướng giao diện được hỗ trợ mới xuất hiện để ngăn điều này.

Nếu một người muốn tự động điều chỉnh các bộ điều khiển chế độ xem khác, điều đó sẽ trở nên phức tạp hơn. Về cơ bản, bộ điều khiển xem "dưới cùng" của bạn phải triển khai phương thức supportedInterfaceOrientations và trả về, dựa trên topViewController hiện tại, mặt nạ bit thích hợp. Điều này có thể được thực hiện với một thường trình truy vấn phương thức "shouldAutorotateToInterfaceOrientation" cũ của topViewController, nhưng nó hơi xấu xí. Và mặc dù chương trình này không yêu cầu sửa đổi mã điều khiển của chế độ xem xoay, bạn KHÔNG cần phải sửa đổi VC chỉ "bên dưới" xoay vòng để thực hiện "supportedInterfaceOrientation", nếu không chế độ xem đó sẽ được xoay khi trả về. (Ít nhất đây là một bản sao đơn giản/dán.) Không ai dường như đã đưa ra một kế hoạch tốt hơn, tổng quát hơn, mặc dù.

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