zoukankan      html  css  js  c++  java
  • iOS7 edgesForExtendedLayout -- 解决冲突 / 系统偏移

    今天在做UISearchBar,UISearchDisplayController时遇到了一个问题,在点击搜索栏时阴影部分的位置出现偏差

    如下图:


    始终觉得很奇怪,后面单独做了一个demo,将同样的代码拷过去发现显示正常的。

    然后再逐一查看代码看到如下:

    1. - (void)viewDidLoad  
    2. {  
    3.     [super viewDidLoad];  
    4.     // Do any additional setup after loading the view.  
    5.     if (OSVersionIsAtLeastiOS7()) {  
    6.         if ([self respondsToSelector:@selector(edgesForExtendedLayout)])  
    7.         {  
    8.             self.edgesForExtendedLayout = UIRectEdgeNone;  
    9.         }  
    10.     }  
    11. }  

    发现可疑之处,Google之iOS 7 教程:让程序同时支持iOS 6和iOS 7,找到答案。

    原因:

    [UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。

    - (void)viewDidLoad中添加如下一行代码:

    1
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    这样问题就修复了。

    2:可以用
    self.nabigationcontroller.navigationbar.translucent = yes;

    3:可以用
    解决冲突 禁止系统偏移  vc.automaticallyadjustsScrollviewInsets =NO

    其实这一切都是automaticallyAdjustsScrollViewInsets在作怪,我们可以先看一下官方文档中对它的描述:

    automaticallyAdjustsScrollViewInsets

    Specifies whether or not the view controller should automatically adjust its scroll view insets.

    @property(nonatomic, assign) BOOL automaticallyAdjustsScrollViewInsets

    Discussion

    Default value is YES, which allows the view controller to adjust its scroll view insets in response to the screen areas consumed by the status bar, navigation bar, and toolbar or tab bar. Set toNO if you want to manage scroll view inset adjustments yourself, such as when there is more than one scroll view in the view hierarchy.

    Availability

    • Available in iOS 7.0 and later.

    Declared In

    UIViewController.h


    哈哈,由此可见,当我们一个界面有多个tableView之类的,要将它设置为NO,完全由自己手动来布局,就不会错乱了.

     

  • 相关阅读:
    sh_09_字典的定义
    sh_08_格式化字符串
    sh_07_元组遍历
    sh_06_元组基本使用
    sh_05_列表遍历
    sh_04_列表排序
    sh_03_列表的数据统计
    图片懒加载
    UA池和ip代理池
    爬虫篇 --- 分布式爬虫
  • 原文地址:https://www.cnblogs.com/yujidewu/p/5736465.html
Copyright © 2011-2022 走看看