今天看autolayout文档,其中有一个例子,就是让一个scroll view的子视图相对于scroll view的父视图固定,从而达到不随着scroll view滚动而滚动的效果。但是自己尝试了下,发现完全没用啊,该滚动还是滚动。视图结构如下:
- controller view
- scroll view
- content view
- floating view
其中floating view 有两个相对于controller view的约束,float view的宽高是固定值。
PS:一般情况下我会认为是自己写错了,但是我在文档的开头部分看到这样一句话:
It’s also not possible to cross any views that have a bounds transform (such as a scroll view). You can think of such views as barriers—there’s an inside world and an outside world, but the inside cannot be connected to the outside by constraints.
大意是说,(虽然很多时候约束可以横跨视图结构),但是对于bounds会发生变形的视图,例如scroll view是不行的。可是,在后面又专门列举了一个上述的例子,我糊涂了。
在PS:我知道可以将floating view添加给controller view,置于scroll view上方一样可以达到悬浮的目的,但我先知道用autolayout行不行。
下面是关于这个例子的文档节选,由于苹果在九月份更新了文档,所以只能截图给大家看了,请看create anchored views inside a sroll view那一段




Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我试了,这可以实现啊。
就算floating view是scroll view的子view,但是只要你的floating位置的约束是相对于controller view的,floating view就不会跟着scroll view滚动的。