iOS11遇到的坑总结

来源:互联网 发布:vb.net 安卓 编辑:程序博客网 时间:2024/06/18 14:26

、iOS 11之前的导航栏的高度是64px(状态条+导航栏),iOS11之后如果设置了prefersLargeTitles = YES(默认NO)则为96pt。所以一般不用管。

、在iOS 11上运行tableView向下偏移64px或者20px,因为iOS 11废弃了automaticallyAdjustsScrollViewInsets,而是给UIScrollView增加了contentInsetAdjustmentBehavior属性。避免这个坑的方法是要判断

if (@available(iOS 11.0, *)) {_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;}else {self.automaticallyAdjustsScrollViewInsets = NO;}

、tableView的sectionHeader、sectionFooter高度与设置不符,因为tableView的estimatedRowHeight、estimatedSectionHeaderHeight、 estimatedSectionFooterHeight三个高度估算属性由默认的0变成了UITableViewAutomaticDimension。最简单的方法就是直接设置为0。

、iPhone X状态条由20px变成了44px,UITabBar由49px变成了83px。设置布局时y直接写成64的就要根据机型设置。可以设置宏

#define Device_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO),

然后再设置。
、MJRefresh在iOS11.0瞬间刷新
升级Xcode 9 + iOS 11后,发现原本没问题的collectionView和tableView像是中了风一样,头部刷新UI出现了错乱。

查阅发现 iOS11弃用了automaticallyAdjustsScrollViewInsets属性,新增contentInsetAdjustmentBehavior来替代它

关于 contentInsetAdjustmentBehavior

@available(iOS 11.0, *)  public enum UIScrollViewContentInsetAdjustmentBehavior : Int {      case automatic // Similar to .scrollableAxes, but will also adjust the top & bottom contentInset when the scroll view is owned by a view controller with automaticallyAdjustsScrollViewContentInset = YES inside a navigation controller, regardless of whether the scroll view is scrollable      case scrollableAxes // Edges for scrollable axes are adjusted (i.e., contentSize.width/height > frame.size.width/height or alwaysBounceHorizontal/Vertical = YES)      case never // contentInset is not adjusted      case always // contentInset is always adjusted by the scroll view's safeAreaInsets  } 

IScrollViewContentInsetAdjustmentBehavior 是一个枚举类型,值有以下几种:

-automatic 和scrollableAxes一样,scrollView会自动计算和适应顶部和底部的内边距并且在scrollView 不可滚动时,也会设置内边距.-scrollableAxes 自动计算内边距.-never不计算内边距-always 根据safeAreaInsets 计算内边距

很显然,我们这里要设置为 never
解决方法:

 //声明tableView的位置 添加下面代码          if (@available(iOS 11.0, *)) {              _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;              _tableView.contentInset = UIEdgeInsetsMake(64, 0, 49, 0);              _tableView.scrollIndicatorInsets = _tableView.contentInset;          }  

还有可能只设置一下never:

        if (@available(iOS 11.0, *)) {              _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;  

.用UISearchController创建的搜索器跳转控制器(巨坑,超后悔),在iOS11有个问题,searchBar的placeholder属性,文字居左,默认是居中的。

解决方法一:
实现方法,自定义UISearchBar:

//  placeholder 和icon 和 间隙的整体宽度// @property (nonatomic, assign) CGFloat placeholderWidth;//  在layoutSubviews里默认先居中if (@available(iOS 11.0, *)) {        [self setPositionAdjustment:UIOffsetMake((field.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch]; } // 实现textfield的代理方法// 开始编辑的时候重置为靠左-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {// 继续传递代理方法     if ([self.delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {        [self.delegate searchBarShouldBeginEditing:self];    }    if (@available(iOS 11.0, *)) {        [self setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];    }    return YES;}// 结束编辑的时候设置为居中-(BOOL)textFieldShouldEndEditing:(UITextField *)textField {     if ([self.delegate respondsToSelector:@selector(searchBarShouldEndEditing:)]     {        [self.delegate searchBarShouldEndEditing:self];    }    if (@available(iOS 11.0, *)) {        [self setPositionAdjustment:UIOffsetMake((textField.frame.size.width-self.placeholderWidth)/2, 0) forSearchBarIcon:UISearchBarIconSearch];    }    return YES;}

解决方法二:
非跳转状态下

self.searchC.searchBar.searchTextPositionAdjustment = UIOffsetMake((CGRectGetWidth(self.searchC.searchBar.frame)-6

点击跳转时,再让文字居左。

self.searchC.searchBar.searchTextPositionAdjustment = UIOffsetMake(0, 0);

.如果我们自己创建tableview而不是继承自uitableviewcontroller的话,那么当我们选择UITableViewStyleGrouped组模式时,我们设置heightForFooterInSection和heightForHeaderInSection不起作用了。
解决方法:当我们设置UITableViewStyleGrouped模式时,那么这时候系统会自动为我们生成一个具有frame的viewForFooter和viewForHeader。因此我们需要手动设置viewForFooterInSection和viewForHeaderInSection。

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{    return 5;}- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {    return 5;}- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{    UIView *view=[[UIView alloc] init];    view.backgroundColor = [UIColor clearColor];    return view;}- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{    UIView *view=[[UIView alloc] init];    view.backgroundColor = [UIColor clearColor];    return view;}

自定义的tableview死活不走代理方法
问题:

-(id)initWithData:(NSDictionary *)data{    self = [super init];    if (self) {        self.delegate = self;        self.dataSource = self;        self.layer.cornerRadius = 5.0;        self.layer.masksToBounds = YES;        self.scrollEnabled = NO;        self.tag = 101;    }    return self;}

解决方法:其实就是上面的第三条,自定义的tableview要设置三个属性,不然就不走。。。。。

-(id)initWithData:(NSDictionary *)data{    self = [super init];    if (self) {        self.delegate = self;        self.dataSource = self;        if (@available(iOS 11.0, *)) {            self.estimatedSectionHeaderHeight = 0;            self.estimatedSectionFooterHeight = 0;            self.estimatedRowHeight = 0;        }        self.layer.cornerRadius = 5.0;        self.layer.masksToBounds = YES;        self.scrollEnabled = NO;        self.tag = 101;    }    return self;}
原创粉丝点击