Требуется изменение ориентации, но только для одного из нескольких представлений в TabBarViewController
-
20-09-2019 - |
Вопрос
В моем приложении есть UITabBarController с пятью вкладками.Мне нужно было повернуть ориентации только для пятой вкладки.Я смог повернуть все пять в альбомную ориентацию, создав подкласс UITabBarController
@implementation TabBarControllerRotate
-(BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight
|| interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationPortrait);
}
@конец
if(tbc == nil){
//tbc = [[UITabBarController alloc] initWithNibName:nil bundle:nil];
tbc = [[TabBarControllerRotate alloc] initWithNibName:nil bundle:nil]; ////// new //////
....
tbc.viewControllers =
[NSArray arrayWithObjects:viewController1, viewController2, viewController3
,viewController4,viewController5, nil];
Теперь мне нужно отключить вращение для viewController1 - 4 ;Я безуспешно пытался сделать это, добавив следующий код к четырем файлам *.m для этих ViewControllers.
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Пожалуйста, посоветуйте мне, как сделать R.Спасибо за чтение, Марк
Решение
Я предполагаю, что есть другие, более простые способы сделать это, но я, наконец, нашел решение.Название пятого ViewController в TabBarController, единственного которому необходимо изменить ориентацию, - "MyNotes"
Внутри SubListViewController.m создается объект TabBarControllerRotate.
tbc = [[TabBarControllerRotate alloc] initWithNibName:nil bundle:nil]; tbc.viewControllers = [NSArray arrayWithObjects:viewController1 , viewController2, viewController3, viewController4, viewController5, nil tbc.delegate = self;
TabBarController является подклассом UITabBarController и включает свойство BOOL назовите bMyNotes.
SubListViewController - это UITabBarControllerDelegate
Каждый раз, когда представление изменяется в TabBarController, отправляется сообщение, а именно - (недействительный)TabBarController: (UITabBarController *)TabBarController didSelectViewController:(UIViewController *)ViewController
Следовательно, SubListViewController.m содержит этот код:
#pragma mark UITabBarControllerDelegate methods - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ BOOL b = (viewController.title == @"MyNotes"); [tbc setBMyNotesTab:b]; //NSLog(@"MyNotesTab == %d", [tbc bMyNotesTab]); }
Тогда TabBarControllerRotate.m содержит:
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation { //NSLog(@"bMyNotesTab is: %d",self.bMyNotesTab); return ( (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationPortrait ) && self.bMyNotesTab );
}