4

plistファイルから取得したものの大きなリストを含むテーブルがあり、それぞれをクリックすると、新しいビューであるxibに移動します。

その.xib内に2つのビューがあります。1つはポートレート用、もう1つはランドスケープ用です。

私のhファイルにはこれがあります:

IBOutlet UIView *portraitView;  
IBOutlet UIView *landscapeView;

@property (nonatomic, retain) IBOutlet UIView *portraitView;  
@property (nonatomic, retain) IBOutlet UIView *landscapeView;

私のmファイルではこれ:

[super viewDidLoad];  
    // Do any additional setup after loading the view from its nib.

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

}

- (void) orientationChanged:(id)object
{
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation];

if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)  
    {  
        self.view = self.portraitView;  
    }  
    else
    {  
        self.view = self.landscapeView;  
    }  
}

- (void)viewDidUnload
{

    [super viewDidUnload];  
    // Release any retained subviews of the main view.  
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation   
{  
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {  

       self.view = portraitView;
    } else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {

       self.view = landscapeView;  
    }  
    return YES;

}

@end

iOS 5ではすべてが完全に機能し、必要に応じて横向きまたは縦向きで表示されていました。

iOS 6アップデートでは、すべてが混乱します。

テーブル(縦)ビューで1つのアイテムをクリックすると、縦に正しく表示されます。横に回転すると、ビューにも正しいビューが表示されますが、テーブルに戻って選択すると、横になります。別のアイテムでは、横向きではなく縦向きのビューが表示されます。

私が同じことをしますが、最初の風景をすると、それは肖像画を示します。

そのため、オリエンテーションは何に対しても機能していません。

ストーリーボードを使用している他のビューにも同じことが起こります。それらは縦向きで、常にそのように表示されます。今では回転し、すべてを縮小し、アプリをゴミ箱として残します。

1- .xibの向きを修正するにはどうすればよいですか?
2-ストーリーボードの向きを修正するにはどうすればよいですか?(それらは静的でしたが、今ではすべてが回転します(コードはまったくありません))

ありがとう。

4

3 に答える 3

13

回避策があると思います。それは醜いですが、動作しています... iOS6 では、Apple は 2 つの違いの XIB ファイルを使用して縦表示と横表示を切り替えることを提案しています。

しかし、2 つの UIView IBOutlet を「切り替える」ことによって iOS 5.0 で許可されていた以前の方法を使用したい場合は、私の醜い作業ソリューションを試すことができます。アイデアは、方向に従ってビューを回転させることです。

1) viewDidLoad で、オリエンテーション通知を購読します。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
 }

2) 通知によって呼び出されるメソッドを追加します。

-(void)orientationChanged:(NSNotification *)object{
    NSLog(@"orientation change");
    UIDeviceOrientation deviceOrientation = [[object object] orientation];
    if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){
        self.view = self.potraitView;
        if(deviceOrientation ==UIInterfaceOrientationPortraitUpsideDown){
            NSLog(@"Changed Orientation To PortraitUpsideDown");
            [self portraitUpsideDownOrientation];
        }else{
            NSLog(@"Changed Orientation To Portrait");
            [self portraitOrientation];
        }
    }else{
        self.view = self.landscapeView;
        if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){
            NSLog(@"Changed Orientation To Landscape left");
            [self landscapeLeftOrientation];
        }else{
            NSLog(@"Changed Orientation To Landscape right");
            [self landscapeRightOrientation];
        }

    }
}

3) 最後に、各方向の回転方法を追加します。

-(void)landscapeLeftOrientation{

    // Rotates the view.
    CGAffineTransform transform = CGAffineTransformMakeRotation(-(3.14159/2));
    self.view.transform = transform;
    // Repositions and resizes the view.
    CGRect contentRect = CGRectMake(0, 0, 480, 320);
    self.view.bounds = contentRect;
}
-(void)landscapeRightOrientation{

    // Rotates the view.
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
    self.view.transform = transform;
    // Repositions and resizes the view.
    CGRect contentRect = CGRectMake(0, 0, 480, 320);
    self.view.bounds = contentRect;
}
-(void)portraitOrientation{

    // Rotates the view.
    CGAffineTransform transform = CGAffineTransformMakeRotation(0);
    self.view.transform = transform;
    // Repositions and resizes the view.
    CGRect contentRect = CGRectMake(0, 0, 320, 480);
    self.view.bounds = contentRect;
}
-(void)portraitUpsideDownOrientation{

    // Rotates the view.
    CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159);
    self.view.transform = transform;
    // Repositions and resizes the view.
    CGRect contentRect = CGRectMake(0, 0, 320, 480);
    self.view.bounds = contentRect;
}

カスタム UIViewController クラスを作成し、このクラスから継承して冗長コードを節約することをお勧めします。ios5 と ios6 の両方のソリューションをサポートする場合は、#endif マクロを使用して両方のコードをコントローラーに含めることができます。

乾杯

于 2012-10-09T12:07:08.220 に答える
1

通知を送受信する必要はありません:

appdelegate.mで次の方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

は常にウィンドウの向きを確認するために呼び出されるため、簡単な方法は、以下に説明するコードをappdelegate.mに含めることです。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

    NSUInteger orientations = UIInterfaceOrientationMaskPortrait;

    if(self.window.rootViewController){
        UIViewController *presentedViewController ;
        if ([self.window.rootViewController isKindOfClass:([UINavigationController class])])
        {
            presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        }
        else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]){
            UITabBarController *controller = (UITabBarController*)self.window.rootViewController;

            id selectedController =  [controller presentedViewController];

            if (!selectedController) {
                selectedController = [controller selectedViewController];
            }

                if ([selectedController isKindOfClass:([UINavigationController class])])
                {
                    presentedViewController = [[(UINavigationController *)selectedController viewControllers] lastObject];
                }
                else{
                    presentedViewController = selectedController;
                }
        }
        else
        {
            presentedViewController = self.window.rootViewController;
        }

        if ([presentedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
            orientations = [presentedViewController supportedInterfaceOrientations];
        }
    }

    return orientations;
}

実装します

- (NSUInteger)supportedInterfaceOrientations

それぞれのビューコントローラで

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice
}

向きの変化に対して突然のアクションを実行するには、次のメソッドを実装します

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
于 2013-02-05T09:46:08.963 に答える