#ios #objective-c
#iOS #objective-c
Вопрос:
Мой UIViewController не вызвал метод shouldAutorotate.
попробовал несколько способов принудительного отображения VC в портретном режиме.
Пример кода приведен ниже :
AppDelegate.m
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.navController = [[UINavigationController alloc] initWithRootViewController:[[VCLogin alloc] init]];
[self.window setRootViewController:self.navController];
[UIApplication sharedApplication].statusBarHidden = YES;
[self.window makeKeyAndVisible];
VCLogin.m
- (BOOL)shouldAutorotate
{
return [self.navigationController.visibleViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationPortraitUpsideDown;
}
И включена ориентация устройства как «Книжная», «Альбомная влево» и «Альбомная вправо»
Любая идея будет оценена по достоинству.
Заранее спасибо.
Комментарии:
1. вам нужно защитить конкретный vc, чтобы он находился в портретном режиме?
2. ДА. Я использую iOS 10 sdk
Ответ №1:
Попробуйте добавить следующий код :
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
//or return UIInterfaceOrientationMaskPortrait , etc as your requirement
}
Комментарии:
1. на самом деле я ищу, чтобы поддерживать только один vc в портретном режиме во всем приложении.
2. создайте этот viewcontroller в раскадровке с sizeclass: компактная ширина и обычная высота
3. К сожалению, мой проект не включен в раскадровку. В нем хранятся только файлы xib.
4. затем используйте autolayout и sizeclasses в этом xib и используйте для него компактную ширину и обычную высоту
Ответ №2:
Я нашел решение.
Добавлен пользовательский класс UINavigationController.
navigationControllerViewController.h
#import <UIKit/UIKit.h>
@interface navigationControllerViewController : UINavigationController
@end
и переопределите следующие методы
navigationControllerViewController.m
- (BOOL)shouldAutorotate {
return [self.visibleViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask )supportedInterfaceOrientations {
return [self.visibleViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.visibleViewController preferredInterfaceOrientationForPresentation];
}
затем назначил этот пользовательский навигационный контроллер внутри
Файл AppDelegate.m
// Replace
self.navController = [[UINavigationController alloc] initWithRootViewController:[[VCMasterView alloc] init]];
// With
self.navController = [[navigationControllerViewController alloc] initWithRootViewController:[[VCMasterView alloc] init]];