我创建了两个用于纵向模式的.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];
}
}
写完这么多代码后,我的应用程序什么都没显示......它只显示黑屏。
请提出一些解决方法。
答案 0 :(得分:2)
一旦尝试这样
[[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];
}
}
答案 1 :(得分:2)
您可以在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
这也是一个很好的!!
或者,
您可以通过创建2个xib来创建旋转控件的更改框架,所有控件具有相同的标记名称,并使一个LayoutManager class
映射标记并获取帧并分配相应的横向/纵向帧(通过从中获取) xibs)。
答案 2 :(得分:0)
-(void)setFramesForPotrait
{
// set frames here
}
-(void)setFramesForLandscaeMode
{
// set frames here
}
-(bool)shouldAutorotate.....
{
return Yes
}
-()willAutoRotate......
{
if(orientation = uiinterfaceOrientationPotrait)
{
[self setFramesForPotrait];
}
else
{
[self setFramesForLandscape];
}
为什么不在单视图控制器中为横向和纵向设置框架