在iOS中的方向中加载不同的xib文件
我创build了两个.xib
文件,一个用于纵向模式,另一个用于横向模式,
每次旋转我想加载相应的.xib
文件,
这是我的代码片段,
ViewAppDelegate.m类
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait | [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown) { self.viewController = [[OrientationViewController alloc] initWithNibName:@"PortraitController" bundle:nil]; UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navigationController; } else{ self.viewController = [[OrientationViewController alloc] initWithNibName:@"LandscapeController" bundle:nil]; UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navigationController; } [self.window makeKeyAndVisible]; return YES; }
ViewController.m类
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ return YES; } -(void)viewWillLayoutSubviews{ if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait | [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown) { [[NSBundle mainBundle] loadNibNamed:@"PortraitController" owner:self options:nil]; } else{ [[NSBundle mainBundle] loadNibNamed:@"LandscapeController" owner:self options:nil]; } }
写了这么多的代码后,我的应用程序显示没有任何内容..它只显示黑屏。
请提出一些解决scheme。
一次尝试像这样
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil];
将此方法添加到应用程序视图控制器中以获取通知。
-(void)changeOrientation { UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil]; } else { [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil]; } }
你可以在viewController.m中使用willRotateToInterfaceOrientation
来实现这个目标
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) ) { [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])] owner: self options: nil]; [self viewDidLoad]; } else { [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@", NSStringFromClass([self class])] owner: self options: nil]; [self viewDidLoad]; } }
你可以检查你的viewwillAppear
方法的方向,如下所示,分别加载xib:
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:YES]; if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){ [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@", NSStringFromClass([self class])] owner: self options: nil]; [self viewDidLoad]; } else { [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])] owner: self options: nil]; [self viewDidLoad]; } }
在这里,我有xibs这样的名字
肖像:viewControllerName
景观:viewControllerName-landscape
我再次调用viewDidload方法来再次加载新的xib。
另一种实现方法是: GPOrientationKit
这也是一个不错的!
要么,
您可以通过创build具有相同标记名称的所有控件创build2个xib并创build一个LayoutManager class
来映射标记并获取帧并分配相应的横向/纵向帧(通过从xib中提取)来创build旋转控件的更改框架。
-(void)setFramesForPotrait { // set frames here } -(void)setFramesForLandscaeMode { // set frames here } -(bool)shouldAutorotate..... { return Yes } -()willAutoRotate...... { if(orientation = uiinterfaceOrientationPotrait) { [self setFramesForPotrait]; } else { [self setFramesForLandscape]; }
为什么不在单个视图控制器中为横向和纵向设置帧