iOS11 适配问题总结

UITableView篇

自定义TableViewCell拖拽事件后,在iOS11上滑动TableView,界面元素会消失。

解决方法:
if (@available(iOS 11, *)) {
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    }

系统默认的左滑删除事件中,左滑的距离可以过长的问题,而且当你full swipe(完全滑动)的时候,系统会自动执行第一个action的handler。


2333461-fc9267cdb472ae54.png

iOS8之后新增了代理方法tableView: editActionsForRowAtIndexPath:和类UITableViewRowAction,可以在这个代理方法中定义所需要的操作按钮(删除、置顶等),这些按钮的类就是UITableViewRowAction。这个类只能定义按钮的显示文字、背景色、和按钮事件。
并且返回数组的第一个元素在UITableViewCell的最右侧显示,最后一个元素在最左侧显示。iOS 11可以给这些按钮添加图片了,而且如果实现了以下两个iOS 11新增的代理方法,将会取代tableView: editActionsForRowAtIndexPath:代理方法:

- ( UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除
    UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:@"delete" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        [self deleteObject:indexPath];
    }];
    deleteAction.image = [UIImage imageNamed:@"del"];
    deleteAction.backgroundColor = [UIColor redColor];

    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
    return config;
}

typedef NS_ENUM(NSInteger, UIContextualActionStyle) {
    UIContextualActionStyleNormal,
    UIContextualActionStyleDestructive
}

UIContextualActionStyle有两种类型,如果是置顶、已读等按钮就使UIContextualActionStyleNormal类型,delete操作按钮可使用UIContextualActionStyleDestructive类型,当使用该类型时,如果是左滑操作,一直滑动某个cell,会直接执行删除操作,不用再点击删除按钮,即系统会自动执行第一个action的handler。当然了,如果产品需求是必须跟之前风格统一只能点“删除”按钮才能删除而且不能左滑距离过长就不能直接这么用了。

解决方法1:

 //iOS11及以上
#ifdef __IPHONE_11_0
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos){
UIContextualAction *action=[UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:@"删除" handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
    [self deleteRecord:indexPath];
}];
action.backgroundColor = [UIColor redColor];
UISwipeActionsConfiguration *config=[UISwipeActionsConfiguration configurationWithActions:@[action]];
config.performsFirstActionWithFullSwipe = NO;
return config;
}
#endif

解决方法2:

仿微信效果通过pan手势自定义左滑事件解决。

详细代码见demo:
https://github.com/WSGNSLog/EditTableView

iOS11 无法跳转设置页--做到了跳转设置页,但不是指定设置页
参照:http://www.jianshu.com/p/527c7098add5

判断ios11 系统的宏这样写 不会报警告

#define IOS11 @available(iOS 11.0, *)

//判断是iPhoneX 的宏

#define is_iPhoneX [UIScreen mainScreen].bounds.size.width == 375.0f && [UIScreen mainScreen].bounds.size.height == 812.0f

keyWindow获取

iOS 11之前通过 [[UIApplication sharedApplication].windows lastObject]获取keyWindow没有问题,iOS11多了一个UIRemoteKeyboardWindow,改为通过[UIApplication sharedApplication].keyWindow获取

po [UIApplication sharedApplication].windows

<__NSArrayM 0x1c085f410>(
; layer = >,
>,
>
)

iPhone X Push过程中TabBar位置上移

UINavigationController的基类重写pushViewController代理方法,在Push的时候修正一下TabBarframe

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.viewControllers.count > 0) {
            viewController.hidesBottomBarWhenPushed = YES;
        }
    [super pushViewController:viewController animated:animated];
    // 修改tabBra的frame
    CGRect frame = self.tabBarController.tabBar.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
    self.tabBarController.tabBar.frame = frame;
}

其他跳动问题解决

在继承于系统UITabBar的子类加上下面代码

#import "XYTabBar.h"

@interface XYTabBar()

@property (nonatomic,assign)UIEdgeInsets oldSafeAreaInsets;

@end

@implementation XYTabBar

- (void) safeAreaInsetsDidChange
{
    [super safeAreaInsetsDidChange];
    if(self.oldSafeAreaInsets.left != self.safeAreaInsets.left ||
       self.oldSafeAreaInsets.right != self.safeAreaInsets.right ||
       self.oldSafeAreaInsets.top != self.safeAreaInsets.top ||
       self.oldSafeAreaInsets.bottom != self.safeAreaInsets.bottom)
    {
        self.oldSafeAreaInsets = self.safeAreaInsets;
        [self invalidateIntrinsicContentSize];
        [self.superview setNeedsLayout];
        [self.superview layoutSubviews];
    }

}

- (CGSize) sizeThatFits:(CGSize) size
{
    CGSize s = [super sizeThatFits:size];
    if(@available(iOS 11.0, *))
    {
        CGFloat bottomInset = self.safeAreaInsets.bottom;
        if( bottomInset > 0 && s.height < 50) {
            s.height += bottomInset;
        }
    }
    return s;
}
@end

参考:http://blog.csdn.net/xuyang844175181/article/details/78134552

Xcode9出现错误safe area layout guide before ios 9 真正解决办法:

正解是选中控制器,右边面板的Builds for 选择iOS9.0 and Later,如下图红框
广为流传的错解是不勾选Use Safe Area Layout Guides,如下图灰框,会导致用不了iOS的这个新功能了
513608-20171124103405468-1557984758.png
UITableView顶部空白

设置:

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)];

你可能感兴趣的:(iOS11 适配问题总结)