美文网首页
iOS7 edgesForExtendedLayout-搜索栏

iOS7 edgesForExtendedLayout-搜索栏

作者: Hedgehogembrace | 来源:发表于2017-03-31 12:44 被阅读0次

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

    如下图:

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

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

    - (void)viewDidLoad

    {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    if (OSVersionIsAtLeastiOS7()) {

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])

    {

    self.edgesForExtendedLayout = UIRectEdgeNone;

    }

    }

    }


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

    原因:

    在iOS 7中,苹果引入了一个新的属性,叫做[UIViewController setEdgesForExtendedLayout:],它的默认值为UIRectEdgeAll。当你的容器是navigation controller时,默认的布局将从navigation bar的顶部开始。这就是为什么所有的UI元素都往上漂移了44pt。

    修复这个问题的快速方法就是在方法- (void)viewDidLoad中添加如下一行代码:

    self.edgesForExtendedLayout = UIRectEdgeNone;

    这样问题就修复了。

    相关文章

      网友评论

          本文标题: iOS7 edgesForExtendedLayout-搜索栏

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