iOS開發中對狀態欄的操作比較少,因為狀態欄是系統級別的View,是受蘋果保護的,不可以隨便更改,不可以隨便遮擋住。
iOS開發中最常見的對狀態欄的操作就是
設置狀態欄的樣式,比如: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];(此樣式的狀態欄中的字為白色,所以用在導航欄為深色的情況下,例如黑色或者紅色的導航欄)
還有
```
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
```
(此樣式的狀態欄中的字為黑色色,所以用在導航欄為淺色色的情況下,比如白色的導航欄)
如果你設置了,發現狀態欄沒反應,那么你要去info.plist中加一個key,這個key為View controller-based status bar appearance,對應的value為NO。這樣每個vc中就可以隨便設置狀態欄的樣式了。
那么實際上我們可以拿到狀態欄這個UIView對象。為了不讓蘋果看到我們用了某某私有API,我們這樣做:
- (UIView*)statusBarView;
{
UIView*statusBar =nil;
NSData*data = [NSDatadataWithBytes:(unsignedchar[]){0x73,0x74,0x61,0x74,0x75,0x73,0x42,0x61,0x72}length:9];
NSString*key = [[NSStringalloc]initWithData:dataencoding:NSASCIIStringEncoding];
idobject = [UIApplicationsharedApplication];
if([objectrespondsToSelector:NSSelectorFromString(key)]) statusBar = [objectvalueForKey:key];
returnstatusBar;
}
調用這個方法就可以拿到系統的狀態欄對象,是個UIView,那么UIView我們最熟悉了,我們可以改變狀態欄的顏色,大小位置(frame)等等。
下面我們要從狀態欄中獲取我們想要的信息
定義一個工具類StatusBarTool,用來獲取網絡類型,運營商,電池電量,顯示的系統時間等信息。那么直接看代碼:
.h文件
#import < Foundation/Foundation.h >
#import < UIKit/UIKit.h >
//0 - 無網絡 ; 1 - 2G ; 2 - 3G ; 3 - 4G ; 5 - WIFI
typedef NS_ENUM(NSUInteger, NetWorkType) {
NetWorkTypeNone=0,
NetWorkType2G=1,
NetWorkType3G=2,
NetWorkType4G=3,
NetWorkTypeWiFI=5,
};
@interface StatusBarTool : NSObject
/**
* @return 當前網絡類型
*/
+(NetWorkType )currentNetworkType;
/**
* @return SIM卡所屬的運營商(公司)
*/
+(NSString *)serviceCompany;
/**
* @return 當前電池電量百分比
*/
+(NSString *)currentBatteryPercent;
/**
* @return 當前時間顯示的字符串
*/
+(NSString *)currentTimeString;
@end
.m實現文件(很重要)
#import “StatusBarTool.h”
@ implementation StatusBarTool
+(NSString *)currentBatteryPercent{
NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@”statusBar”] valueForKeyPath:@”foregroundView”] subviews];
for (id info in infoArray)
{
if ([info isKindOfClass:NSClassFromString(@”UIStatusBarBatteryPercentItemView”)])
{
NSString *percentString = [info valueForKeyPath:@”percentString”];
NSLog(@”電量為:%@”,percentString);
return percentString;
}
}
return @”“;
}
+(NetWorkType )currentNetworkType{
NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@”statusBar”] valueForKeyPath:@”foregroundView”] subviews];
NetWorkType type;
for (id info in infoArray)
{
if ([info isKindOfClass:NSClassFromString(@”UIStatusBarDataNetworkItemView”)]) {
type = [[info valueForKeyPath:@”dataNetworkType”] integerValue];
NSLog(@”—-%lu”, (unsigned long)type);
return (NetWorkType)type;
}
}
return NetWorkTypeNone;
}
+(NSString *)currentTimeString{
NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@”statusBar”] valueForKeyPath:@”foregroundView”] subviews];
for (id info in infoArray)
{
if ([info isKindOfClass:NSClassFromString(@”UIStatusBarTimeItemView”)])
{
NSString *timeString = [info valueForKeyPath:@”timeString”];
NSLog(@”當前顯示時間為:%@”,timeString);
return timeString;
}
}
return @”“;
}
+(NSString *)serviceCompany{
NSArray *infoArray = [[[[UIApplication sharedApplication] valueForKeyPath:@”statusBar”] valueForKeyPath:@”foregroundView”] subviews];
for (id info in infoArray)
{
if ([info isKindOfClass:NSClassFromString(@”UIStatusBarServiceItemView”)])
{
NSString *serviceString = [info valueForKeyPath:@”serviceString”];
NSLog(@”公司為:%@”,serviceString);
return serviceString;
}
}
return @”“;
}
@end
用StatusBarTool這個類的時候
NSString *company = [StatusBarTool serviceCompany];
NSLog(@”company = %@”,company);
company = 中國移動
NetWorkType type = [StatusBarTool currentNetworkType];
NSLog(@”type = %ld”,type);
type = 5(5代表Wi-Fi)
/*typedef NS_ENUM(NSUInteger, NetWorkType) {
NetWorkTypeNone=0,
NetWorkType2G=1,
NetWorkType3G=2,
NetWorkType4G=3,
NetWorkTypeWiFI=5,
};*/
NSString *batteryPercent = [StatusBarTool currentBatteryPercent];
NSLog(@”batteryPercent = %@”,batteryPercent);
batteryPercent ?? = 96%
NSString *timeString = [StatusBarTool currentTimeString];
NSLog(@”timeString = %@”,timeString);
timeString = 12:57