美文网首页
XCode9 + iOS11环境下UIScrollView出现的

XCode9 + iOS11环境下UIScrollView出现的

作者: LearningCoding | 来源:发表于2017-09-27 18:33 被阅读204次

    对于XCode9 + iOS11环境下 UITableView、UIScrollView向下偏移的问题
    iOS11增加了安全区域:
    SafeAreaInsets值
    在使用系统的navigationBar的情况下:
    SafeAreaInsets值为(64,0,0,0)
    在未使用或者navigationBar设置为隐藏的时候:
    SafeAreaInsets值为(20,0,0,0)
    如果也使用了系统的tabbar:
    SafeAreaInsets值为(64,0,49,0)或(20,0,49,0)
    解决办法一:
    根据iOS11新增的两个属性:adjustContentInset 和 contentInsetAdjustmentBehavior
    该方式可以解决WKWebView偏移的一些问题

        if (@available(iOS 11.0, *)) {
            self.additionalSafeAreaInsets = UIEdgeInsetsMake(0, 0, 0, 0);
            // 如果使用了navigationBar隐藏的情况
            // self.navigationController.additionalSafeAreaInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
    

    办法二:
    根据取代automaticallyAdjustsScrollViewInsets属性的contentInsetAdjustmentBehavior属性

        if (@available(iOS 11.0, *)) {
            self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // 设置内边距
            self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            // 设置滚动条的内边距
            self.webView.scrollView.scrollIndicatorInsets = self.webView.scrollView.contentInset;
        } else {
            self.automaticallyAdjustsScrollViewInsets = false;
        }
    

    @property(nonatomic,assign) BOOL automaticallyAdjustsScrollViewInsets API_DEPRECATED_WITH_REPLACEMENT("Use UIScrollView's contentInsetAdjustmentBehavior instead", ios(7.0,11.0),tvos(7.0,11.0)); // Defaults to YES
    iOS11 中, UIViewController的automaticallyAdjustsScrollViewInsets属性已经不再使用,我们需要使用UIScrollView的 contentInsetAdjustmentBehavior 属性来替代它.
    UIScrollViewContentInsetAdjustmentBehavior 是一个枚举类型,值有以下几种:
    automatic 和scrollableAxes一样,scrollView会自动计算和适应顶部和底部的内边距并且在scrollView 不可滚动时,也会设置内边距.
    scrollableAxes 自动计算内边距.
    never不计算内边距
    always 根据safeAreaInsets 计算内边距
    很显然,我们这里要设置为 never

    相关文章

      网友评论

          本文标题:XCode9 + iOS11环境下UIScrollView出现的

          本文链接:https://www.haomeiwen.com/subject/ouwsextx.html