gpt4 book ai didi

objective-c - UIView 在所有不随屏幕旋转的东西前面?

转载 作者:行者123 更新时间:2023-11-29 11:11:29 24 4
gpt4 key购买 nike

我有这个代码:

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

问题是,当应用程序旋转时,前面的 View 不会旋转。它只是保持纵向。

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

最佳答案

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

The root view controller for the window.

@property(nonatomic, retain) UIViewController *rootViewController

Discussion
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller’s view as the content view of the window. If the window has an existing view hierarchy, the old views are removed before the new ones are installed.

The default value of this property is nil.

Availability
Available in iOS 4.0 and later.

Declared In
UIWindow.h

因为您应该提供这个 Root View Controller ,所以您应该能够将它添加到 rootViewContoller 的 View 中,并正确处理输出旋转。


另一种解决方案可能是,在呈现 View 时将 windo 与自定义的 windo 交换,并使用另一个 View Controller 。你可以在 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

关于objective-c - UIView 在所有不随屏幕旋转的东西前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11388248/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com