[英]different orientations on different views using a NavigationController
我將我的NavigationController子類化,並添加了以下代碼:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft |UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
我有2個視圖控制器。 在沒有旋轉功能的情況下,如何在一個上強制使用橫向模式,在另一個上強制使用縱向模式? (在plist中啟用了所有命令)
假設您使用的是iOS6,請更改自定義導航控制器以使其具有此功能(此解決方案似乎僅適用於模式或全屏演示):
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
和
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
然后在要添加橫向的控制器中
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (BOOL)shouldAutorotate
{
return YES;
}
在另一個
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return YES;
}
我僅考慮iOS 6.0及更高版本的輪換。 在這里為UINaviagtionController創建類別可以是一個好方法。 因此,請按照下列步驟
步驟1.將這段代碼放入您的AppDelegate.m中
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskAll;
}
步驟2.為UINaviagtionController創建類別
方向
#import <Foundation/Foundation.h>
@interface UINavigationController (Orientation)
@end
方向
#import "Orientation.h"
@implementation UINavigationController (Orientation)
- (BOOL)shouldAutorotate {
if (self.topViewController != nil)
return [self.topViewController shouldAutorotate];
else
return [super shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
if (self.topViewController != nil)
return [self.topViewController supportedInterfaceOrientations];
else
return [super supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.topViewController != nil)
return [self.topViewController preferredInterfaceOrientationForPresentation];
else
return [super preferredInterfaceOrientationForPresentation];
}
步驟3.現在創建一個強制初始方向的UIViewController類。 對於肖像
PortraitVC.h
#import <UIKit/UIKit.h>
@interface PortraitVC : ViewController
@end
PortraitVC.m
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
同樣對於風景
景觀VC
#import <UIKit/UIKit.h>
@interface LandscapeVC : ViewController
@end
景觀VC
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
第4步。現在,您要僅在縱向模式下強制且沒有旋轉的原始ViewController,在您的viewDidLoad中編寫此代碼
PortraitVC *c = [[PortraitVC alloc] init];
[self presentViewController:c animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];
並像這樣設置方向
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
如果只想在橫向模式下不旋轉而強制使用原始ViewController,請在viewDidLoad中編寫此代碼
LandscapeVC *c = [[LandscapeVC alloc] init];
[self presentViewController:c animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];
並像這樣設置方向
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
我這樣做是因為僅當我們使用presentViewController或presentModalViewController時才調用“ preferredInterfaceOrientationForPresentation”。 並設置初始方向“ preferredInterfaceOrientationForPresentation”必須被調用。
希望這可以幫助。
因此,經過許多嘗試和錯誤,我找到了可以使用的解決方案。
在您的AppDelegate中:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
這樣會將方向控制權交給根視圖控制器。
在您的DataManager.h中 (或者您正在使用的任何其他單例也可以通過NSUserDefaults完成。
@property (nonatomic, strong) NSString *currentView;
在您的ViewController.m(根目錄)中:
-(NSUInteger)supportedInterfaceOrientations
{
if([[DataManager sharedDataManager].currentView isEqualToString:@"Trailer"])
{
return UIInterfaceOrientationMaskLandscapeRight;
}
else if([[DataManager sharedDataManager].currentView isEqualToString:@"Menu"])
{
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskPortrait;
}
從根ViewController,您可以訪問應用程序中所有ViewControlles的所有方向。 如果要將一個ViewController
限制為橫向,將另一個ViewController
限制為縱向,則非常有用。 您需要添加到每個ViewController的唯一事情是在ViewWillAppear
內部,您要為ViewController命名的方式如下:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[DataManager sharedDataManager].currentView = @"Menu";
}
使用此解決方案,您將不會收到像在當前\\ dissmis視圖解決方案中強制旋轉那樣迅速在視圖之間切換的煩人警告。 也許有一種方法可以更好地識別ViewController,我很想聽聽如何。 我也很想聽聽您是否對此解決方案有任何疑問。 謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.