How to add an icon into iphone/ipod status bar?

2019-07-09 03:00发布

问题:

Does Apple not allow developers to add an icon into a status bar?

I followed code from a book. The code is simple:

@interface UIApplication (extended) 
- (void) addStatusBarImageNamed:(NSString *)aName; 
- (void) removeStatusBarImageNamed:(NSString *)aName; 

@end 

- (void)performAction{
    if (xxx) {
        [[UIApplication sharedApplication]addStatusBarImageNamed:@"Default_EN.png"];
    }
    else {
        [[UIApplication sharedApplication]addStatusBarImageNamed:@"Default_EC.png"];

    }
}

But it gives the following feedback :

-addStatusBarImageNamed: is deprecated. Doing nothing.

What can I do?

回答1:

In Classes/YourViewController.m, the addStatusBarImageNamed:removeOnExit: method needs to be overwritten with this.

- (void) addStatusBarImageNamed:(NSString*)image removeOnExit: (BOOL) remove {
if(_statusbarimage!=nil && _responds) {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"statusBarEnabled"] integerValue] == 1)
[self removeStatusBarImageNamed:_statusbarimage];
statusbarimage=image;
}
if (_responds) {
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"statusBarEnabled"] integerValue] == 1)
[super addStatusBarImageNamed:image removeOnExit: remove];
}
}  

See if it works fine.



回答2:

To my best knowledge, this isn't permitted within the SDK, but there could be the possibilities that they could have some private API to do so but so far they haven't exposed those, I think you are'nt able to add icon in status bar. If someone know please correct me .