【发布时间】:2026-02-17 13:35:01
【问题描述】:
我创建了一个新的 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
有人帮忙吗?
【问题讨论】:
-
- (void)show { if (![self isKeyWindow]) { self.prevKeyWindow = [UIApplication sharedApplication].keyWindow; [self makeKeyAndVisible];您必须在应用程序委托文件中提供此代码。我的意思是您初始化窗口的位置。
-
现在你正试图显示一个看起来像弹出窗口的窗口。它不起作用。我没有尝试它。无论如何都最好
-
这里是初始化代码:
PopoverView *pop = [[PopoverView alloc] initWithFrame:CGRectMake(40, 40, 80, 80)]; [pop show];