1.获取设备方向
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait, // 设备垂直竖向方向放置, 并且home键在下方
UIDeviceOrientationPortraitUpsideDown, //设备垂直竖向方向放置, 并且home键在上方
UIDeviceOrientationLandscapeLeft, // 设备垂直横向方向放置, 并且home键在右方
UIDeviceOrientationLandscapeRight, // 设备垂直横向方向放置, 并且home键在左方
UIDeviceOrientationFaceUp, // 设备水平放置, 屏幕在上方
UIDeviceOrientationFaceDown // 设备水平放置, 屏幕在下方
} (TVOS不可用)
判断当前设备方向
//但这个有个弊端,就是没有设备方向改变通知时,返回UIDeviceOrientationUnknown
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait){
NSLog(@"当前设备是垂直竖向,并且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(@"当前设备是垂直竖向,并且home键在下方");
}
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
2.获取设备电池状态
typedef NS_ENUM(NSInteger, UIDeviceBatteryState) {
UIDeviceBatteryStateUnknown,
UIDeviceBatteryStateUnplugged, // 未充电,使用电池
UIDeviceBatteryStateCharging, // 充电中,并且电量小于 100%
UIDeviceBatteryStateFull, // 充电中, 电量已达 100%
} __TVOS_PROHIBITED; // TVOS不可用,iOS3之后可用
判断当前设备电池状态
[UIDevice currentDevice].batteryMonitoringEnabled = YES; //开启电量监控,默认不开启
if ([[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateCharging){
NSLog(@"正在充电中,且电量不足100");
}
//当前电量,小数[0,1] ,如果[[UIDevice currentDevice] batteryState] == UIDeviceBatteryStateUnknown时,则为1.0
NSLog(@"当前电量%f",[[UIDevice currentDevice] batteryLevel]);
3.获取设备类型
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.修改某个界面的横竖屏状态
上述方法只能获取设备的当前屏幕状态,不能修改,如果想要修改,请参考下面的方法
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之后
//是否支持旋转
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
//支持的旋转方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
- 全部界面采用同一种
如果window.rootViewController
是UINavigationViewController的话,只要在导航控制器中设置完毕之后,所有push
出来的viewController
,都是和NavigationViewController
一样
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- 多个界面,采用各自的屏幕需求,在
NavigationViewController
中实现下面的方法,指定每个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];
}