首先贡献几个宏定义,下文也有用到!ios
//动态获取设备宽度 #define IPHONE_WIDTH [UIScreen mainScreen].bounds.size.width #define IPHONE_HEIGHT [UIScreen mainScreen].bounds.size.height //iPhone X判断 #define IPHONE_X ([UIScreen mainScreen].bounds.size.width == 375 && [UIScreen mainScreen].bounds.size.height == 812) //状态栏高度 #define StatusBar_Height ([UIApplication sharedApplication].statusBarFrame.size.height + 44) //底部Tabbar高度 #define Tabbar_Height (IPHONE_X ? (49 + 34) : 49) //iPhone X底部高度 #define Tabbar_Bottom_Margin (IPHONE_X ? 34 : 0)
正常来讲不适配的话iPhone X的上下边会有黑边,好比这样的:json
固然这张图是我后期截的,底部的Tabbar会有白边,若是没有作过处理,正常来讲是不会有的。
解决办法,在项目设置里面直接用LaunchScreen.xib
或者LaunchScreen.storyboard
进行配置启动图,或者改变LaunchImage.launchimage
里的Contents.json
配置app
{ "extent":"full-screen", "idiom":"iphone", "subtype":"2436h", "filename":"iPhone_5.8.png", "minimum-system-version":"11.0", "orientation":"portrait", "scale":"3x" }
这样项目就会完整显示了。以下图less
相似这样:iphone
好比像这样:
解决办法:经过重绘图片高度达到铺满效果ide
//设置全局属性 UINavigationBar *appearance = [UINavigationBar appearance]; UIImage *image = [UIImage imageNamed:@"nav_bg"]; if (IPHONE_X) image = [image scaleToSize:CGSizeMake(IPHONE_WIDTH, StatusBar_Height)]; [appearance setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; [appearance setShadowImage:[[UIImage alloc] init]];
- (UIImage *)scaleToSize:(CGSize)size { UIGraphicsBeginImageContext(size); [self drawInRect:CGRectMake(0, 0, size.width, size.height)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
重绘后的效果以下:动画
注意看底部tabbar
的跳动。
解决办法:新建一个继承UINavigationController
的类,而后重写spa
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { [super pushViewController:viewController animated:animated]; if (IPHONE_X) { // 修改tabBra的frame CGRect frame = self.tabBarController.tabBar.frame; frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height; self.tabBarController.tabBar.frame = frame; } }
注意看导航栏下的UITableView
有一个往上偏移的动画。code
缘由:automaticallyAdjustsScrollViewInsets
这个属性在iOS11中已经被弃用,咱们须要使用UIScrollView
的contentInsetAdjustmentBehavior
属性来替代它。blog
typedef NS_ENUM(NSInteger, UIScrollViewContentInsetAdjustmentBehavior) { UIScrollViewContentInsetAdjustmentAutomatic, // Similar to .scrollableAxes, but for backward compatibility will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewInsets = YES inside a navigation controller, regardless of whether the scroll view is scrollable UIScrollViewContentInsetAdjustmentScrollableAxes, // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES) UIScrollViewContentInsetAdjustmentNever, // contentInset is not adjusted UIScrollViewContentInsetAdjustmentAlways, // contentInset is always adjusted by the scroll view's safeAreaInsets } API_AVAILABLE(ios(11.0),tvos(11.0)); UIScrollViewContentInsetAdjustmentBehavior 是一个枚举类型,值有如下几种: UIScrollViewContentInsetAdjustmentAutomatic 和UIScrollViewContentInsetAdjustmentScrollableAxes同样,scrollView会自动计算和适应顶部和底部的内边距而且在scrollView 不可滚动时,也会设置内边距. UIScrollViewContentInsetAdjustmentScrollableAxes 自动计算内边距. UIScrollViewContentInsetAdjustmentNever不计算内边距 UIScrollViewContentInsetAdjustmentAlways 根据safeAreaInsets 计算内边距 很显然,咱们这里要设置为 UIScrollViewContentInsetAdjustmentNever
我在MainNavigationController
类的initialize
方法中去设置UIScrollView
全局属性。固然,也能够在合适的地方去添加属性设置。
UIScrollView
类的上下留有滚动边距(顶部留有49,底部留有34)的问题,一样也可经过此方法解决。+ (void)initialize { //兼容ios11,修复push和pop时UIScrollView的子类会发生一个偏移动画 if (@available(iOS 11.0, *)) { [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } }
可能升级到ios11后,你发现有的APP没法使用定位,并且连定位权限申请的弹窗都不会出现。那是由于ios的定位权限中新增了一个NSLocationAlwaysAndWhenInUseUsageDescription
字段,须要在info.plist中添加相应的代码。
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> <string>定位权限描述,本身能够根据需求修改</string>
相应的弹窗也发生了变化