参考资料:
App界面适配iOS11(包括iPhoneX的奇葩尺寸)
有关iOS11和iPhoneX的适配问题
适配iOS11&iPhoneX的一些坑
判断iPhoneX
swift 判断iPhoneX
extension UIDevice {
//是否iPhoneX
public class func isIphoneX() -> Bool {
if UIScreen.main.bounds.height == 812 {
return true
}
return false
}
}
OC 判断iPhoneX
@interface UIDevice(extension)
+ (BOOL)isiphoneX;
@end
@implementation UIDevice(extension)
+ (BOOL)isiphoneX{
if([UIScreen mainScreen].bounds.size.height == 812){
return YES;
}else{
return NO;
}
}
@end
iOS 11 禁止tableView自动偏移(整体上移或下移)
self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;
if (@available(iOS 11.0, *)){
[self.tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
解决iPhoneX Push过程中TabBar位置上移方法
封装一个基类UINavigationController,这里名字设置为:MyNavigationController。在MyNavigationController中覆盖父类方法。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
//适配iPhone X Push过程中TabBar位置上移
if (self.viewControllers.count > 0) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
// 修改tabBar的frame
CGRect frame = self.tabBarController.tabBar.frame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
self.tabBarController.tabBar.frame = frame;
}
调整iPhoneX tabbar图标位置
这里引用ESTabbar的关键代码,其实是修改UITabbar的itemPositioning属性。
public var itemCustomPositioning: ESTabBarItemPositioning? {
didSet {
if let itemCustomPositioning = itemCustomPositioning {
switch itemCustomPositioning {
case .fill:
itemPositioning = .fill
case .automatic:
itemPositioning = .automatic
case .centered:
itemPositioning = .centered
default:
break
}
}
self.reload()
}
}