此篇文章講述iOS中的狀態(tài)欄
iOS開(kāi)發(fā)中對(duì)狀態(tài)欄的操作比較少,因?yàn)闋顟B(tài)欄是系統(tǒng)級(jí)別的View壁查,是受蘋(píng)果保護(hù)的觉至,不可以隨便更改,不可以隨便遮擋住睡腿。如果違反這些蘋(píng)果的規(guī)范康谆,那么APP將有可能上不了架。筆者就增寫(xiě)過(guò)一個(gè)APP嫉到,因狀態(tài)欄被提示框遮擋而被拒(杯具)了沃暗。
iOS開(kāi)發(fā)中最常見(jiàn)的對(duì)狀態(tài)欄的操作就是設(shè)置狀態(tài)欄的樣式,比如: [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];(此樣式的狀態(tài)欄中的字為白色何恶,所以用在導(dǎo)航欄為深色的情況下孽锥,例如黑色或者紅色的導(dǎo)航欄)
還有
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];(此樣式的狀態(tài)欄中的字為黑色色,所以用在導(dǎo)航欄為淺色色的情況下细层,比如白色的導(dǎo)航欄)
如果你設(shè)置了惜辑,發(fā)現(xiàn)狀態(tài)欄沒(méi)反應(yīng),那么你要去info.plist中加一個(gè)key疫赎,這個(gè)key為View controller-based status bar appearance盛撑,對(duì)應(yīng)的value為NO。這樣每個(gè)vc中就可以隨便設(shè)置狀態(tài)欄的樣式了捧搞。
那么實(shí)際上我們可以拿到狀態(tài)欄這個(gè)UIView對(duì)象抵卫。為了不讓蘋(píng)果看到我們用了某某私有API狮荔,我們這樣做:
- (UIView*)statusBarView;
{
UIView *statusBar = nil;
NSData *data = [NSData dataWithBytes:(unsigned char []){0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x42, 0x61, 0x72} length:9];
NSString *key = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
id object = [UIApplication sharedApplication];
if ([object respondsToSelector:NSSelectorFromString(key)]) statusBar = [object valueForKey:key];
return statusBar;
}
調(diào)用這個(gè)方法就可以拿到系統(tǒng)的狀態(tài)欄對(duì)象,是個(gè)UIView介粘,那么UIView我們最熟悉了殖氏,我們可以改變狀態(tài)欄的顏色,大小位置(frame)等等姻采。
下面我們要從狀態(tài)欄中獲取我們想要的信息
定義一個(gè)工具類(lèi)StatusBarTool雅采,用來(lái)獲取網(wǎng)絡(luò)類(lèi)型,運(yùn)營(yíng)商慨亲,電池電量婚瓜,顯示的系統(tǒng)時(shí)間等信息。那么直接看代碼:
.h文件
#import < Foundation/Foundation.h >
#import < UIKit/UIKit.h >
//0 - 無(wú)網(wǎng)絡(luò) ; 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 當(dāng)前網(wǎng)絡(luò)類(lèi)型
*/
+(NetWorkType )currentNetworkType;
/**
*
*
* @return SIM卡所屬的運(yùn)營(yíng)商(公司)
*/
+(NSString *)serviceCompany;
/**
*
*
* @return 當(dāng)前電池電量百分比
*/
+(NSString *)currentBatteryPercent;
/**
*
*
* @return 當(dāng)前時(shí)間顯示的字符串
*/
+(NSString *)currentTimeString;
@end
.m實(shí)現(xiàn)文件(很重要)
#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(@”當(dāng)前顯示時(shí)間為:%@”,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這個(gè)類(lèi)的時(shí)候
NSString *company = [StatusBarTool serviceCompany];
NSLog(@”company = %@”,company);
company = 中國(guó)移動(dòng)
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
好了刑棵,到此我們已經(jīng)拿到了網(wǎng)絡(luò)狀態(tài)闰渔,運(yùn)營(yíng)商,電池電量百分比铐望,時(shí)間顯示等信息了冈涧。