无法在状态栏,uiwindow,目标C上显示视图

时间:2014-04-10 20:41:18

标签: objective-c ios7 uiwindow

我试图通过执行以下操作在状态栏上添加叠加层:

ViewController实现

- (IBAction)addViewOnTop:(id)sender {
    StatusBarOverlayWindow *overlay = [[ACStatusBarOverlayWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    overlay.hidden = NO;
    [overlay makeKeyAndVisible];
}

StatusBarOverlayWindow头文件

#import <UIKit/UIKit.h>

@interface StatusBarOverlayWindow : UIWindow {
}
@end

StatusBarOverLayWindow实现

@implementation StatusBarOverlayWindow
- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Place the window on the correct level and position
        self.windowLevel = UIWindowLevelStatusBar + 1;
        self.frame = frame;
        self.backgroundColor = [UIColor orangeColor];
    }
    return self;
}
@end

当我点击按钮时,根本没有任何反应。 有没有人对这个问题有任何想法。请指导,谢谢

1 个答案:

答案 0 :(得分:2)

尝试保留对StatusBarOverlayWindow的强引用,而不会在addViewOnTop:方法完成后变量超出范围。物业在这里运作良好。

示例:

@interface ViewController : UIViewController

@property (nonatomic, strong) StatusBarOverlayWindow *overlayWindow;

@end

然后在你的方法中:

- (IBAction)addViewOnTop:(id)sender {
    self.overlayWindow = [[ACStatusBarOverlayWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
    overlay.hidden = NO;
    [overlay makeKeyAndVisible];
}