makeKeyWindowAndVisible 不起作用

发布于 2024-12-23 17:04:27 字数 1189 浏览 1 评论 0原文

我创建一个新的 UIWindow 并想在窗口中显示一个视图,如弹出视图,但它没有显示。(我阅读的代码 SVProgressHUD 以供参考)

代码如下:

在 .h 文件中

#import <UIKit/UIKit.h>

@interface PopoverView : UIWindow

@property (nonatomic, strong) NSArray *items;

- (void)show;

@end

在 .m 文件中

@interface PopoverView () 

@property (nonatomic, assign) UIWindow *prevKeyWindow;

@end


@implementation PopoverView

@synthesize items=_items;
@synthesize prevKeyWindow;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        self.windowLevel = UIWindowLevelAlert;
        // Initialization code
        UILabel *view = [[UILabel alloc] initWithFrame:frame];
        view.backgroundColor = [UIColor blackColor];
        [self addSubview:view];
        self.backgroundColor = [UIColor blackColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return self;
}

- (void)show {
    if (![self isKeyWindow]) {
        self.prevKeyWindow = [UIApplication sharedApplication].keyWindow;
        [self makeKeyAndVisible];
    }
}

@end

有人帮忙吗?

I create a new UIWindow and want to show a view in the window like popover view, but It do not show up.(The code I read SVProgressHUD for reference)

the code is below:

In .h file

#import <UIKit/UIKit.h>

@interface PopoverView : UIWindow

@property (nonatomic, strong) NSArray *items;

- (void)show;

@end

In .m file

@interface PopoverView () 

@property (nonatomic, assign) UIWindow *prevKeyWindow;

@end


@implementation PopoverView

@synthesize items=_items;
@synthesize prevKeyWindow;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        self.windowLevel = UIWindowLevelAlert;
        // Initialization code
        UILabel *view = [[UILabel alloc] initWithFrame:frame];
        view.backgroundColor = [UIColor blackColor];
        [self addSubview:view];
        self.backgroundColor = [UIColor blackColor];
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return self;
}

- (void)show {
    if (![self isKeyWindow]) {
        self.prevKeyWindow = [UIApplication sharedApplication].keyWindow;
        [self makeKeyAndVisible];
    }
}

@end

Anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2024-12-30 17:04:27

PopoverView变量必须是全局变量,如果是局部变量,UIWindow将被销毁,所以你看不到它(很快出现和消失)。

The PopoverView variable must be a global variable, if it's a local variable, the UIWindow will be destroyed, so you can't see it(appeared and disappeared very quickly).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文