44

ユーザーがUITableViewCellのボタンをタップして関連する画像をズームするコアアニメーションを使用して、デバイス全体(UIWindow)をカバーするUIViewを使用して、画像のズームイン/アウト効果をサポートします。

ズームは問題なく実行されていますが、デバイスが横向きになっているにもかかわらず、サブビューがまだ縦向きモードになっている理由を理解できませんでした。以下の図:

ここに画像の説明を入力

ナビゲーション コントローラーはありますが、このビューは UIWindow に直接追加されています。

4

6 に答える 6

121

考えられる原因のいくつかについては、こちらをご覧ください:
Technical Q&A QA1688 - UIViewController がデバイスと共に回転しないのはなぜですか?

あなたの状況では、おそらくビューを別のサブビューとしてウィンドウに追加しているという事実です。最初のサブビューのみが回転イベントを取得します。できることは、最初のウィンドウ サブビューのサブビューとして追加することです。

UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) 
    window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];    
于 2010-07-30T04:56:54.057 に答える
83

問題

iOS 6 以降、最上位のビュー コントローラー (UIApplication オブジェクトと並んで) のみが、デバイスの向きの変化に応じて回転するかどうかの決定に参加します。

https://developer.apple.com/library/content/qa/qa1688/_index.html

ソリューション

AGWindowViewという名前のポッドをオープンソース化しました。
回転やフレームの変更は自動的に処理されるため、心配する必要はありません。

コード

SDK と iOS システム バージョンの任意の組み合わせをサポートします。関連するコードはここにあります: https://github.com/hfossli/AGWindowView/blob/master/Source/AGWindowView.m

于 2011-02-10T18:14:02.820 に答える
4

keyWindow の最初のサブビューを取得するためのヘルパー プロパティとメソッドを持つ UIApplication のカテゴリを作成しました。とにかくオーバーレイしたいビューです。UIViewController によって管理されるビューをそのビューに追加すると、 shouldRotateToInterfaceOrientation: メソッドが呼び出されます。

UIApplication+WindowOverlay.h

#import <UIKit/UIKit.h>

@interface UIApplication(WindowOverlay)

    @property (nonatomic, readonly) UIView *baseWindowView;

    -(void)addWindowOverlay:(UIView *)view;

@end

UIApplication+WindowOverlay.m

#import "UIApplication+WindowOverlay.h"

@implementation UIApplication(WindowOverlay)

    -(UIView *)baseWindowView{
        if (self.keyWindow.subviews.count > 0){
            return [self.keyWindow.subviews objectAtIndex:0];
        }
        return nil;
    }

    -(void)addWindowOverlay:(UIView *)view{
        [self.baseWindowView addSubview:view];
    }
@end

使用方法は次のとおりです。

//at the top of the file...or in {yourproject}.pch
#import "UIApplication+WindowOverlay.h

//in a method:

UIView *view = [UIView new];

UIView *window = [UIApplication sharedApplication].baseWindowView;

view.frame = window.bounds;

[window addSubview:view];

//or

[[UIApplication sharedApplication] addWindowOverlay:view];
于 2011-03-01T23:37:26.780 に答える
1

これは、ビューが UIWindow に直接追加されたことに言及したためです。したがって、回転するメソッドがナビゲーション コントローラーに対して呼び出されても、UIView には何も起こりません。ビュー コントローラー ビューのサブビューである場合、UIView は回転します。何らかの理由でこれができない場合。次に、このメソッドをオーバーライドできます。

// This method is called every time the device changes orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
}

また、向きが変わるたびに、ビューの向きも変わります。

于 2010-03-24T15:59:48.500 に答える
0

ビューがウィンドウに直接追加されるという同様の問題がありました。たぶんこれが役に立ちます:ウィンドウに追加した後に UIView を自動的にサイズ変更する

于 2010-04-22T17:47:19.380 に答える
0

この問題を解決した別の解決策。

現在の方向を定義します。

@interface AJImageCollectionViewController (){
    UIInterfaceOrientation _currentOrientation;
}
@end

次に、viewWillLayoutSubviews で向きを確認します。

- (void)viewWillLayoutSubviews {
    [self checkIfOrientationChanged];
}

- (void)checkIfOrientationChanged {
    UIInterfaceOrientation newOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    BOOL newOrientationIsPortrait = UIInterfaceOrientationIsPortrait(newOrientation);
    BOOL oldOrientationIsPortrait = UIInterfaceOrientationIsPortrait(_currentOrientation);

    // Check if the orientation is the same as the current
    if(newOrientationIsPortrait != oldOrientationIsPortrait){
        _currentOrientation = newOrientation;
        // Do some stuff with the new orientation
    }
}
于 2013-05-30T09:46:08.800 に答える