Mỗi cái nhìn cũ đẩy lên điều hướng điều khiển ngăn xếp phải hỗ trợ các định hướng tương tự. Điều này có nghĩa là không thể có một số bộ điều khiển xem chỉ hỗ trợ ảnh chân dung và các bộ điều khiển khác chỉ hỗ trợ cảnh quan.Nói cách khác, tất cả các bộ điều khiển chế độ xem trên cùng một ngăn điều khiển điều hướng phải trả lại như nhau trong ủy quyền:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Nhưng có một giải pháp đơn giản cho điều này! Đây là một ví dụ để đi từ chân dung đến phong cảnh. Đây là các bước để làm điều đó và dưới đây là mã để hỗ trợ nó.
- Tạo bộ điều khiển chế độ xem ‘giả mạo’ sẽ nằm trong trình điều khiển điều hướng phụ. Bộ điều khiển xem này sẽ hỗ trợ cảnh quan.
- Tạo một thể hiện mới của một
UINavigationController
, thêm một thể hiện của quan điểm điều khiển 'giả' root và một thể hiện của điều khiển xem phong cảnh của bạn như xem thứ hai điều khiển
- Trình bày dụ
UINavigationController
như phương thức từ bộ điều khiển xem mẹ
Đầu tiên, tạo một bộ điều khiển xem mới (FakeRootViewController) với mã này:
@interface FakeRootViewController : UIViewController
@property (strong, nonatomic) UINavigationController* parentNavigationController;
@end
@implementation FaceRootViewController
@synthesize parentNavigationController;
// viewWillAppear is called when we touch the back button on the navigation bar
(void)viewWillAppear:(BOOL)animated {
// Remove our self from modal view though the parent view controller
[parentNavigationController dismissModalViewControllerAnimated:YES];
}
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
Dưới đây là đoạn code để trình bày các điều khiển xem mà bạn muốn hiển thị trong chế độ nằm ngang:
FakeRootViewController* fakeRootViewController = [[FakeRootViewController alloc] init];[fakeRootViewController.navigationItem setBackBarButtonItem:backButton]; // Set back button
// The parent navigation controller is the one containing the view controllers in portrait mode.
fakeRootViewController.parentNavigationController = parentNavigationController;
UINavigationController* subNavigationController = // Initialize this the same way you have initialized your parent navigation controller.
UIViewController* landscapeViewController = // Initialize the landscape view controller
[subNavigationController setViewControllers:
[NSArray arrayWithObjects:fakeRootViewController,
landscapeViewController, nil] animated:NO];
[_navigationController presentModalViewController:subNavigationController animated:YES];
Hãy nhớ rằng landscapeViewController cũng cần phải có thực hiện điều này:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
Nguồn
2012-06-09 06:11:41
bạn có thể chia sẻ mã mẫu không? –