1

.xibポートレートモード用とランドスケープモード用の2つのファイルを作成しました。

ローテーションごとに、それぞれ.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

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

以下のようにメソッドの方向を確認し、viewwillAppearxibsをそれぞれロードできます。

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

ここでは、
Portrait:viewControllerName
Landscape:viewControllerName-landscapeのような名前xibsがあります

そして、新しいxibを再度ロードするためにviewDidloadメソッドを再度呼び出しました。

これを達成する別の方法は次のとおりです 。GPOrientationKit
これも素晴らしい方法です。

または、

すべてのコントロールが同じタグ名を持つ2つのxibを作成しLayoutManager class、タグをマップしてフレームをフェッチし、それぞれのランドスケープ/ポートレートフレームを割り当てることで(xibsからフェッチすることにより)、回転時にコントロールの変更フレームを作成できます。

于 2013-03-21T07:05:47.780 に答える
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 に答える
0
-(void)setFramesForPotrait
{

// set frames here

}

-(void)setFramesForLandscaeMode
{

// set frames here

}

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

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

単一のView Controllerで横向きと縦向きの両方のフレームを設定しないのはなぜですか

于 2013-03-21T06:26:06.577 に答える