tranvutuan
tranvutuan

Reputation: 6109

can not show view on top of status bar, uiwindow, objective C

I am trying to put an overlay on top of status bar by doing the following :

ViewController implementation

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

StatusBarOverlayWindow header file

#import <UIKit/UIKit.h>

@interface StatusBarOverlayWindow : UIWindow {
}
@end

StatusBarOverLayWindow implementation

@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

When I click a button, nothing happens at all. Does anyone have any thoughts about this issue. Please guide, thanks

Upvotes: 0

Views: 648

Answers (1)

danielbeard
danielbeard

Reputation: 9149

Try keeping a strong reference to the StatusBarOverlayWindow, without that the variable will go out of scope after the addViewOnTop: method is complete. A property will work well here.

Example:

@interface ViewController : UIViewController

@property (nonatomic, strong) StatusBarOverlayWindow *overlayWindow;

@end

Then in your method:

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

Upvotes: 2

Related Questions