且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用自动布局向 UITableView 添加子视图不会设置其框架

更新时间:2023-12-05 20:43:52

怀疑是因为 UITableView 是一个 UIScrollView 并且自动布局在使用建立自己的边界系统的视图(例如滚动视图或表格)时有一些警告查看).

Suspect it's because the UITableView is a UIScrollView and auto layout has a few caveats when working with views that establish their own bounds systems (such as the scroll view or table view).

在覆盖视图上设置的约束是这样的,它被约束到滚动视图的顶部、右侧、底部和左侧边缘,因此被约束到滚动视图的内容区域而不是滚动视图的框架.

The constraints set on the overlay view are such that it is constrained to the top, right, bottom, and left edges of the scroll view, and thus to the content area of the scroll view rather than the scroll view's frame.

https://developer.apple.com/library/ios/technotes/tn2154/_index.html自动布局指南:使用滚动视图:

滚动视图的边缘或边距之间的约束和它的内容附加到滚动视图的内容区域.

Constraints between the edges or margins of the scroll view and its content attach to the scroll view’s content area.

不是将覆盖视图限制到内容区域,而是将其限制到滚动视图的框架.根据上面的参考资料,这可以通过将覆盖视图的宽度/高度限制为滚动视图来完成,或者通过将覆盖视图限制为滚动视图之外的视图来完成.

Instead of constraining overlay view to the content area, constrain it to the scroll view's frame instead. According to the above references, this can be done either by constraining overlay view's width/height to the scroll view, or by constraining the overlay view to a view outside of the scroll view.