1

私はこのコードを持っています:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    imageView.image = [UIImage imageNamed:@"image.png"];

    UIApplication *app = [UIApplication sharedApplication];

    [app addSubViewOnFrontWindow:imageView];

..。

- (void)addSubViewOnFrontWindow:(UIView *)view {
    int count = [self.windows count];
    UIWindow *w = [self.windows objectAtIndex:count - 1];
    [w addSubview:view];
}

問題は、アプリが回転しても前面のビューが回転しないことです。それはただ肖像画のままです。

これをデバイスの他の部分と一緒に正常に回転させるにはどうすればよいですか?

4

3 に答える 3

1

私はこれを試していませんUIWindowが、プロパティがありますrootViewController

ウィンドウのルートビューコントローラ。

@property(nonatomic, retain) UIViewController *rootViewController

説明
ルートビューコントローラは、ウィンドウのコンテンツビューを提供します。View Controllerをこのプロパティに(プログラムでまたはInterface Builderを使用して)割り当てると、ViewControllerのビューがウィンドウのコンテンツビューとしてインストールされます。ウィンドウに既存のビュー階層がある場合、新しいビューがインストールされる前に古いビューが削除されます。

このプロパティのデフォルト値はnilです。

可用性
iOS4.0以降で利用できます。


UIWindow.hで宣言

このルートビューコントローラーを提供する必要があるため、rootViewContollerのビューに追加して、外側の回転を正しく処理できるはずです。


別の解決策は、別のビューコントローラを使用して、ビューを表示しながらウィンドウをカスタムのものと交換することです。このトリックの動作は、TSAlertViewの実装で確認できます。

- (void) show
{
    [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate date]];

    TSAlertViewController* avc = [[[TSAlertViewController alloc] init] autorelease];
    avc.view.backgroundColor = [UIColor clearColor];

    // $important - the window is released only when the user clicks an alert view button
    TSAlertOverlayWindow* ow = [[TSAlertOverlayWindow alloc] initWithFrame: [UIScreen mainScreen].bounds];
    ow.alpha = 0.0;
    ow.backgroundColor = [UIColor clearColor];
    ow.rootViewController = avc;
    [ow makeKeyAndVisible];

    // fade in the window
    [UIView animateWithDuration: 0.2 animations: ^{
        ow.alpha = 1;
    }];

    // add and pulse the alertview
    // add the alertview
    [avc.view addSubview: self];
    [self sizeToFit];
    self.center = CGPointMake( CGRectGetMidX( avc.view.bounds ), CGRectGetMidY( avc.view.bounds ) );;
    self.frame = CGRectIntegral( self.frame );
    [self pulse];

    if ( self.style == TSAlertViewStyleInput )
    {
        [self layoutSubviews];
        [self.inputTextField becomeFirstResponder];
    }
}

@interface TSAlertOverlayWindow : UIWindow
{
}
@property (nonatomic,retain) UIWindow* oldKeyWindow;
@end

@implementation  TSAlertOverlayWindow
@synthesize oldKeyWindow;

- (void) makeKeyAndVisible
{
    self.oldKeyWindow = [[UIApplication sharedApplication] keyWindow];
    self.windowLevel = UIWindowLevelAlert;
    [super makeKeyAndVisible];
}

- (void) resignKeyWindow
{
    [super resignKeyWindow];
    [self.oldKeyWindow makeKeyWindow];
}

//

@end
于 2012-07-09T23:44:57.417 に答える
1

新しいrootviewControllerを使用して新しいウィンドウを追加し、サブスクライブします

[[NSNotificationCenter defaultCenter] addObserver:(id) selector:(SEL) name:UIDeviceOrientationDidChangeNotification object:nil];
于 2012-07-10T00:04:04.410 に答える
0

Windowsは自動回転しません。これまで。UIViewControllerには自動回転が組み込まれているため、このimageViewをUIViewControllerのビューに追加する必要があります。

UIWindowを使用してすべてのビューを保持する場合は、回転を処理するための独自の変換コードを作成する必要があります。

于 2012-07-09T01:50:57.250 に答える