1

我创建了两个.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];
    }

}

写了这么多代码后,我的应用程序什么也没显示……它只显示黑屏。

请提出一些解决方案。

4

3 回答 3

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];
    }

}
于 2013-03-21T06:41:48.550 回答
2

你可以willRotateToInterfaceOrientation在 viewController.m 中使用来实现这个目标

-(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];
    }
}

在这里,我有名称 xib,例如
Portrait: viewControllerName
Landscape: viewControllerName-landscape

我再次调用 viewDidload 方法再次加载新的 xib。

实现此目的的另一种方法是: GPorientationKit
它也是一个不错的方法!

或者,

您可以通过创建 2 个具有相同标签名称的控件来创建旋转时控件的更改框架,并制作一个LayoutManager class来映射标签并获取框架并分配相应的横向/纵向框架(通过从 xibs 获取)。

于 2013-03-21T07:05:47.780 回答
0
-(void)setFramesForPotrait
{

// set frames here

}

-(void)setFramesForLandscaeMode
{

// set frames here

}

-(bool)shouldAutorotate.....
{
return Yes
}

-()willAutoRotate......
{
if(orientation = uiinterfaceOrientationPotrait)
{
[self setFramesForPotrait];
}
else
{
[self setFramesForLandscape];
}

为什么不在单视图控制器中为横向和纵向设置框架

于 2013-03-21T06:26:06.577 回答