UIView面前的一切都没有旋转屏幕?

时间:2012-07-09 01:47:55

标签: objective-c ios uiview rotation

我有这段代码:

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

问题是,当应用程序旋转时,前面的视图不会旋转。它只是留在画像中。

如何让它与设备的其余部分正常旋转?

3 个答案:

答案 0 :(得分:1)

我没有尝试过,但UIWindow有一个属性rootViewController

  

窗口的根视图控制器。

     

@property(nonatomic, retain) UIViewController *rootViewController

     

<强>讨论
  根视图控制器提供了内容视图   窗口。将视图控制器分配给此属性(或者   以编程方式或使用Interface Builder)安装视图   控制器的视图作为窗口的内容视图。如果窗口有   在现有视图层次结构中,旧视图在新视图之前被删除   已安装。

     

此属性的默认值为nil。

     

<强>状况
  适用于iOS 4.0及更高版本。

     

声明
  UIWindow.h

由于您应该提供此根视图控制器,您应该能够将其添加到rootViewContoller的视图中,并正确处理旋转。


另一个解决方案可能是,您使用另一个视图控制器在显示视图的同时将windo与自定义交换。你可以在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

答案 1 :(得分:1)

使用新的rootviewController添加一个新窗口,并将其订阅到

[[NSNotificationCenter defaultCenter] addObserver:(id) selector:(SEL) name:UIDeviceOrientationDidChangeNotification object:nil];

答案 2 :(得分:0)

Windows不会自动旋转。永远。您应该将此imageView添加到UIViewController的视图中,因为UIViewController已内置自动旋转。

如果您想使用UIWindow来保存所有视图,则需要编写自己的转换代码来处理旋转。

相关问题