iOS开发——Autolayout的Top Layout guide问题

http://kyleduo.com/?p=302

写这篇博客之前,真想大吼一声:Top Layout Guide,你到底是什么鬼!

The topLayoutGuide and bottomLayoutGuide properties indicate the location of the top or bottom
bar edges in a view controller’s view. If bars should overlap the top or bottom of a view, you can use
Interface Builder to position the view relative to the bar by creating constraints to the bottom of
topLayoutGuide or to the top of bottomLayoutGuide. (If no bars should overlap the view, the bottom
of topLayoutGuide is the same as the top of the view and the top of bottomLayoutGuide is the same
as the bottom of the view.) Both properties are lazily created when requested.

问题描述

在学习UIPageViewController的过程中,按照《【译】如何使用Storyboard创建UIPageViewController》编写引导页Demo,但是做完的效果和Demo效果有一些出入,效果分别为:正确效果错误效果

简单说就是当滑动到下一页的时候,下一页会有一个缩小效果,但是这个效果并不是手动添加的。

分析问题

之前的那篇《iOS学习笔记——UIScrollView的坑和填坑》中提到了Autolayout的问题:自动添加Constraints导致出错。这次要说的问题和上次都和一个东西有关——Top Layout Guide。

Top Layout Guide用于自动布局的辅助,在Storyboard中可以看到Top Layout Guide作为ViewController的属性存在,也就是topLayoutGuide,官方文档对这个属性的Discussion是:

topLayoutGuide属性表示不希望被透明的状态栏或导航栏遮挡的内容范围的最高位置。这个属性的值是它的length属性的值(topLayoutGuide.length),这个值可能由当前的ViewController或这个ViewController所属的NavigationController或TabBarController决定,有如下情况:

  • 一个独立的ViewController,不包含于任何其他的ViewController。如果状态栏可见,topLayoutGuide表示状态栏的底部,否则表示这个ViewController的上边缘。

  • 包含于其他ViewController的ViewController不对这个属性起决定作用,而是由容器ViewController决定这个属性的含义:

    • 如果导航栏(Navigation Bar)可见,topLayoutGuide表示导航栏的底部。

    • 如果状态栏可见,topLayoutGuide表示状态栏的底部。

    • 如果都不可见,表示ViewController的上边缘。

这部分还比较好理解,总之是屏幕上方任何遮挡内容的栏的最底部。

通过对ViewController的生命周期消息进行跟踪,在下一页显示出来前,首先调用下一页ViewController的viewDidLoad方法,也就是加载过程,此时topLayoutGuide值为0;而显示的消息viewWillAppear:和布局的消息viewWillLayoutSubview在放开手指之后才会发送,而这个时候已经完全显示了,topLayoutGuide的值为20,也就是在状态栏下边缘。

当松手的时候,topLayoutGuide的值发生的这一个变化,就导致我们看到了一个突然缩小的效果。

解决问题

到现在为止,找到了两个解决方法,不过并不完美。

方法一:在设置自动布局约束时,不使用Top Layout Guide,而是使用父View的上边缘。

说方法二之前,还要在说一下AutoLayout的设置,默认添加的约束是这样的:

屏幕快照 2014-10-22 下午3.48.30

注意第二项Top Layout Guide.Bottom,也就是约束到topLayoutGuide的下边缘,也就是说topLayoutGuide是有高度的!!

再次查文档中对length的解释:

The top layout guide indicates the distance, in points, between the top of a view controller’s view and the bottom of the bottommost bar that overlays the view.

可以看到是指当前ViewController的上边缘到遮挡内容的Bar的下边缘,那么topLayoutGuide的top和bottom也就是这两个边缘了。

方法二:自动布局的约束中,将Top Layout Guide.Bottom改为Top Layout Guide.Top。

这两个方法达到的效果是一样的,都是让Label以内容ViewController的上边缘进行约束,也就不会有变化了。Demo中没有这个问题,是因为Demo没有使用Autolayout。

但是也正是如此,两个方法都不完美。内容ViewController是动态加载并嵌入到UIPageViewController中的,而topLayoutGuide又是一个只读属性,不能嵌入到UIPageViewController之前进行设置。

完美的方法应该是在加载内容ViewController的同时,告知内容ViewController它将被嵌入到另一个容器当中,而这个容器包含一个Status Bar,所以应该重新判断topLayoutGuide的值,但遗憾的是目前还没有找到这种完美的解决方案。如果你知道,希望能告诉我,谢谢!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值