IOS11适配

一、定位失效

苹果新增加了一项NSLocationAlwaysAndWhenInUseUsageDescription隐私权限,原有的 NSLocationAlwaysUsageDescription 被降级为 NSLocationWhenInUseUsageDescription。
解决方案:在infoplist文件里里增加NSLocationAlwaysAndWhenInUseUsageDescription 和 NSLocationWhenInUseUsageDescription两个,如果需要支持iOS10的话,增加NSLocationAlwaysUsageDescription

二、相册权限

ios11使用相机功能,原有项目crash。
ios11之后苹果对相册的隐私权限作了调整,原来的NSPhotoLibraryUsageDescription 调整成了NSPhotoLibraryAddUsageDescription
解决方案:在infoplist文件里添加NSPhotoLibraryAddUsageDescription

三、ScrollView相关控件,比如tableview、webView、collectionView等控件顶部会有一定距离的偏移。

    webview.frame=CGRectMake(0, 0, 375, 667);
    [self.view addSubview:webview];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];

运行后如图:未充满屏幕下移了20


WechatIMG5.jpeg

原因是:iOS 11中Controller的automaticallyAdjustsScrollViewInsets属性被废弃了,决定scrollview的内容与边缘距离的是adjustedContentInset属性,而不再是contentInset。当视图被状态栏导航条被覆盖,系统会自系统自动调整了SafeAreaInsets值,进而影响adjustedContentInset值,会对adjustedContentInset值进行了调整,所以导致tableView的内容到边缘的距离发生了变化,导致下移了20pt。

解决方案:关闭系统的自动调整计算
webview.scrollView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever

四、tableview相关


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    self.view.backgroundColor=[UIColor redColor];
    UITableView *tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667) style:UITableViewStyleGrouped];
    tableview.delegate=self;
    tableview.dataSource=self;
    [self.view addSubview:tableview];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{//设置每组里有多少行
    return 1;
}
//去除头部的默认间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
//去除尾部的默认间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * chongzu =@"Cell";//重用机制标识
    
    UITableViewCell* cell =[tableView dequeueReusableCellWithIdentifier:chongzu];//根据重用标r识,到重用池找对应的cell
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:chongzu];//创建一个cell,设置其样式以及其标识
    }
    cell.backgroundColor=[UIColor redColor];
    cell.textLabel.text=[NSString stringWithFormat:@"%li",(long)indexPath.row];
    return cell;//将设置好的cell返回
}
@end

老铁们应该都知道默认tableView开头和结尾是有间距的,在开发中不需要这样的间距,我们通常会返回0.01这样很小的头部和尾部视图高度,在ios11之前是没问题的,然我们在ios11运行一下发现并没卵用。
原因分析:
ios11后estimatedSectionHeaderHeight ,estimatedSectionFooterHeight
这些估算高度默认值都是UITableViewAutomaticDimension,估算机制处于开启状态,代码中我们只返回高度却没有返回对应的视图,这两个返回高度的代理方法是不会执行的。只要返回对应的视图就好了。
解决方案:
方案一 :返回对应的视图

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

方案二:关闭估算机制

    tableview.estimatedSectionHeaderHeight=0;
    tableview.estimatedSectionFooterHeight=0;

有时你也会发现cell莫名其妙的自适应高度了,那是因为ios11后tableview的行高默认不在是44了而是UITableViewAutomaticDimension是自动计算的,如果你需要自适应只需要重设rowheight的值就行了。
五、关于自定义返回按钮

    1. 替换系统返回按钮图片并隐藏返回按钮文字
+(void)initialize{
    
    UIImage *backButtonImage = [[UIImage imageNamed:@"back"]
                                resizableImageWithCapInsets:UIEdgeInsetsMake(0, 18, 0, 0)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage
                                                      forState:UIControlStateNormal
                                                    barMetrics:UIBarMetricsDefault];
    
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
                                                         forBarMetrics:UIBarMetricsDefault];
}

ios11以下运行正常显示:

BA95CE00-E9EA-4ECD-996D-6E29D71AE818.png

而ios11以上运行返回图片不居中


9C5D58E3-853D-4456-A7D2-94AE25BEF98F.png

原因分析:iOS 11 中setBackButtonTitlePositionAdjustment:UIOffsetMake没法把按钮移出navigation bar。

解决方法是设置navigationController的backIndicatorImage和backIndicatorTransitionMaskImage

+(void)initialize{
        UIImage *backButtonImage = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
         [UINavigationBar appearance].backIndicatorImage = backButtonImage;
         [UINavigationBar appearance].backIndicatorTransitionMaskImage = backButtonImage;
       //将返回文字去掉
        [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor clearColor]} forState:UIControlStateNormal];
}

或者这样

+(void)initialize{
        UIImage *backButtonImage = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
         [UINavigationBar appearance].backIndicatorImage = backButtonImage;
         [UINavigationBar appearance].backIndicatorTransitionMaskImage = backButtonImage;
       [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(-300, 0) forBarMetrics:UIBarMetricsDefault];
}

  • 2.自定义UIBarButtonItem
    在iOS7之后,我们在设置UINavigationItem的leftBarButtonItem,rightBarButtonItem的时候都会造成位置的偏移,虽然不大,但是跟UI的设计或者国人的习惯有点区别,通常我们添加一个宽度为负值的UIBarButtonItem
    UIButton *btn=[[UIButton alloc]init];
    btn.frame=CGRectMake(0, 0, 60, 40);
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitle:@"设置" forState:UIControlStateNormal];
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithCustomView:btn];
    UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpace.width =-20;
    self.navigationItem.rightBarButtonItems=@[fixedSpace, item];

但是这些在iOS 11中都无效了!,在ios11后改动相当大的就是导航栏的部分,在原来的已经复杂的不要的图层中又新增了新的图层.那么我这里解决方案是自定义个NavigationBar。
参考:https://www.jianshu.com/p/5ec6e0cc5036

@implementation HLTNavigationBar

- (void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"%@",self.subviews);
    for (UIView *view in self.subviews) {//适配ios11
        if ([NSStringFromClass(view.class) containsString:@"ContentView"]) {
            view.layoutMargins = UIEdgeInsetsZero;//可修正iOS11之后的偏移
            NSLog(@"%@",view.subviews);
        }else{//适配iosios10
            if ([view isKindOfClass:[Backbutton class]]) {
                CGRect frame = view.frame;
                frame.origin.x=0;
                view.frame=frame;
            }
        }
    }
}

六、ios11的滑动删除
IOS11新增了两个代理方法,可以给这些按钮添加图片了。如果实现了新增的Swipe actions的代理方法将会取代(tableView: editActionsForRowAtIndexPath:)
1.向左滑动在右边显示(常用的左滑删除)

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    /* UIContextualActionStyle有两种类型:
    UIContextualActionStyleNormal,//置顶、删除、已读都可以使用该类型
    UIContextualActionStyleDestructive//删除类型可使用,从左到右一直滑cell,不用点击删除按钮就可以直接执行删除操作
    */
    //1.创建UIContextualAction对象
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //[self.titleArr removeObjectAtIndex:indexPath.row];
        completionHandler (YES);
    }];
    //2.给滑动按钮添加背景、图片
    deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
    deleteRowAction.backgroundColor = [UIColor blueColor];
    
   //3.返回滑动按钮
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
}

2、左、右都可以滑
左滑是系统删除按钮,右滑可以是自定义的滑动按钮

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView leadingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    UIContextualAction *deleteRowAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"取消" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //[self.titleArr removeObjectAtIndex:indexPath.row];
        completionHandler (YES);
    }];
    deleteRowAction.image = [UIImage imageNamed:@"icon_del"];
    deleteRowAction.backgroundColor = [UIColor blueColor];
    
    UIContextualAction *RowAction1 = [UIContextualAction contextualActionWithStyle:  UIContextualActionStyleNormal title:@"关注" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        //[self.titleArr removeObjectAtIndex:indexPath.row];
        completionHandler (YES);
    }];
   RowAction1.image = [UIImage imageNamed:@"icon_del"];
    RowAction1.backgroundColor = [UIColor greenColor];

    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction,RowAction1]];
    return config;
    }

七、iPhone X适配
iPhone X 和 iPhone 8 的宽度一致,在垂直方向上多了145pt,状态栏不再是20而是44,没有了 Home 键,iPhone X 的底部预留给系统功能的一个区域 - Home Indicator,这部分的高度是34pt。首先看以下代码感受下:

  • 一、简单感受适配
    UILabel *lable=[[UILabel   alloc]init];
    lable.backgroundColor=[UIColor redColor];
    [self.view addSubview:lable];
    lable.text=@"12344444444";
    lable.textAlignment=NSTextAlignmentCenter;
    lable.frame=CGRectMake(0, 20, self.view.frame.size.width, 30);

iphone8的运行效果:


B567F8E0-F918-4D64-A9C2-2B1A725E3CEA.png

iphone X的运行效果:

6BBDDAB2-5CE3-4368-992E-79DF59B69E0C.png

很明显iPhone X的y值不能写死20了,我们也需要考虑下44高度状态栏的iphone X了,下面像这样简单适配下就好了:

  1. frame的方式
// UIScreen width.
#define  ScreenWidth   [UIScreen mainScreen].bounds.size.width

// UIScreen height.
#define  ScreenHeight  [UIScreen mainScreen].bounds.size.height

// iPhone X
#define  iPhoneX (ScreenWidth == 375.f && ScreenHeight == 812.f ? YES : NO)

// Status bar height.
#define  StatusBarHeight      (iPhoneX ? 44.f : 20.f)

@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *lable=[[UILabel   alloc]init];
    lable.backgroundColor=[UIColor redColor];
    [self.view addSubview:lable];
    lable.text=@"12344444444";
    lable.textAlignment=NSTextAlignmentCenter;
    lable.frame=CGRectMake(0, StatusBarHeight , self.view.frame.size.width, 30);
    NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
}
  1. Autolayout的方式
    Autolayout 视图的 top 和 bottom 一般参照的是 Top Layout Guide 和 Bottom Layout Guide. iOS11 废弃了改用安全区域(SafeArea)做参考来做适配.
适配前的代码:
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {         
      make.left.right.equalTo(self.view);
      make.height.equalTo(@30);
      make.top.equalTo(self.view).offset(20);
    }];
适配后的代码:
   [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        //以安全区域的顶部为参考作约束
 make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        //哈哈,运行下就不用判断是20还是44了
   
        make.left.right.equalTo(self.view);
        make.height.equalTo(@30);
    }];

3.注意iphoneX底部系统功能区域34的高度不要被控件遮挡了:
例如你这样

   self.view.backgroundColor=[UIColor redColor];
    UILabel *lable=[[UILabel   alloc]init];
    lable.backgroundColor=[UIColor greenColor];
    [self.view addSubview:lable];
    lable.text=@"12344444444";
    lable.textAlignment=NSTextAlignmentCenter;
    lable.frame=CGRectMake(0,ScreenHeight-30, self.view.frame.size.width, 30);
974D1DF1-0220-44D0-ADE6-5FCAB4FBF894.png

因此适配就还需要个宏了。
适配如下:

#import "ViewController.h"
// UIScreen width.
#define  ScreenWidth   [UIScreen mainScreen].bounds.size.width

// UIScreen height.
#define  ScreenHeight  [UIScreen mainScreen].bounds.size.height

// iPhone X
#define  iPhoneX (ScreenWidth == 375.f && ScreenHeight == 812.f ? YES : NO)

// Status bar height.
#define  StatusBarHeight      (iPhoneX ? 44.f : 20.f)

//  safe bottom margin.
#define  SafeBottomMargin         (iPhoneX ? 34.f : 0.f)
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    UILabel *lable=[[UILabel   alloc]init];
    lable.backgroundColor=[UIColor greenColor];
    [self.view addSubview:lable];
    lable.text=@"12344444444";
    lable.textAlignment=NSTextAlignmentCenter;
    lable.frame=CGRectMake(0,ScreenHeight-30-SafeBottomMargin, self.view.frame.size.width, 30);
}

或者你这样(哈哈,我就喜欢这样)

#import "ViewController.h"
#import "Masonry.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor redColor];
    UILabel *lable=[[UILabel   alloc]init];
    lable.backgroundColor=[UIColor greenColor];
    [self.view addSubview:lable];
    lable.text=@"12344444444";
    lable.textAlignment=NSTextAlignmentCenter;
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        make.height.equalTo(@30);
    }];

}
  • 二、适配
    待续................
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容