1.獲取設(shè)備方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // 設(shè)備垂直豎向方向放置, 并且home鍵在下方
UIDeviceOrientationPortraitUpsideDown, //設(shè)備垂直豎向方向放置, 并且home鍵在上方
UIDeviceOrientationLandscapeLeft, // 設(shè)備垂直橫向方向放置, 并且home鍵在右方
UIDeviceOrientationLandscapeRight, // 設(shè)備垂直橫向方向放置, 并且home鍵在左方
UIDeviceOrientationFaceUp, // 設(shè)備水平放置, 屏幕在上方
UIDeviceOrientationFaceDown // 設(shè)備水平放置, 屏幕在下方
} (TVOS不可用)
判斷當(dāng)前設(shè)備方向
//但這個有個弊端,就是沒有設(shè)備方向改變通知時,返回UIDeviceOrientationUnknown
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
NSLog(@"當(dāng)前設(shè)備是垂直豎向旧困,并且home鍵在下方");
}
//豎屏吼具,包括UIDeviceOrientationPortrait || UIDeviceOrientationPortraitUpsideDown
if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) {
NSLog(@"豎屏");
}
//橫屏锥债,包括UIDeviceOrientationLandscapeLeft || UIDeviceOrientationLandscapeRight
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
NSLog(@"橫屏");
}
//所以推薦下面的方式
if (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
NSLog(@"當(dāng)前設(shè)備是垂直豎向哮肚,并且home鍵在下方");
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
2.獲取設(shè)備電池狀態(tài)
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged, // 未充電,使用電池
UIDeviceBatteryStateCharging, // 充電中,并且電量小于 100%
UIDeviceBatteryStateFull, // 充電中, 電量已達 100%
} __TVOS_PROHIBITED; // TVOS不可用鲁纠,iOS3之后可用
判斷當(dāng)前設(shè)備電池狀態(tài)
[UIDevice currentDevice].batteryMonitoringEnabled = YES; //開啟電量監(jiān)控情龄,默認(rèn)不開啟
if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging){
NSLog(@"正在充電中,且電量不足100");
}
//當(dāng)前電量,小數(shù)[0,1] ,如果[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown時专酗,則為1.0
NSLog(@"當(dāng)前電量%f",[[UIDevice currentDevice] batteryLevel]);
3.獲取設(shè)備類型
typedef NS_ENUM(NSInteger, UIUserInterfaceIdiom) {
UIUserInterfaceIdiomUnspecified = -1,
UIUserInterfaceIdiomPhone NS_ENUM_AVAILABLE_IOS(3_2), // 0,iPhone and iPod touch style UI
UIUserInterfaceIdiomPad NS_ENUM_AVAILABLE_IOS(3_2), // 1,iPad style UI
UIUserInterfaceIdiomTV NS_ENUM_AVAILABLE_IOS(9_0), // 2,Apple TV style UI
UIUserInterfaceIdiomCarPlay NS_ENUM_AVAILABLE_IOS(9_0), //3, CarPlay style UI
};
NSLog(@"%ld",(long)[UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) //判斷是否為iPhone或者iPod
4.修改某個界面的橫豎屏狀態(tài)
上述方法只能獲取設(shè)備的當(dāng)前屏幕狀態(tài),不能修改佑笋,如果想要修改蒋纬,請參考下面的方法
iOS6之前
// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0) __TVOS_PROHIBITED;
示例
//始終保持豎屏
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
iOS6之后
//是否支持旋轉(zhuǎn)
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//支持的旋轉(zhuǎn)方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
- 全部界面采用同一種
如果window.rootViewController
是UINavigationViewController的話关摇,只要在導(dǎo)航控制器中設(shè)置完畢之后,所有push
出來的viewController
脂凶,都是和NavigationViewController
一樣
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- 多個界面,采用各自的屏幕需求冠桃,在
NavigationViewController
中實現(xiàn)下面的方法食听,指定每個ViewController
按照自己的需求改變,在ViewController
中覆寫此方法
- (BOOL)shouldAutorotate {
if (self.topViewController != nil) return [self.topViewController shouldAutorotate];
else return [super shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations {
if (self.topViewController != nil) return [self.topViewController supportedInterfaceOrientations];
else return [super supportedInterfaceOrientations];
}