作者:图林根の烤肠,如有纰漏欢迎指出。
文章来源:https://www.mahong.me/archives/155
最近论文终于交掉,就剩下答辩,时间也富裕不少,没事上来自己瞎鼓捣下 iOS 编程。
如上图所示,由于涉及到 NavigationController,发现添加的文本距离顶部总是有不可避免的间隙,哪怕是pin 到顶部距离为0依旧如此。解决的办法很简单,参考了YouXianMing的博客内容,,在 view 加载前只需一行代码:
override func viewWillAppear(animated: Bool) { self.edgesForExtendedLayout = UIRectEdge.None }
文档说明为:
The extended edges to use for the layout.
This property is applied only to view controllers that are embedded in a container such as UINavigationController. The window’s root view controller does not react to this property. The default value of this property is UIRectEdgeAll.
大概的意思为这个edgesForExtendedLayout属性只适用于集成了边界容器的 controller,例如UINavigationController。默认值为UIRectEdgeAll,这里它会使UIViewController 的边界扩展64px,就是状态栏20px 与导航栏44px 的和,这就是为什么即使设置UITextView 距离顶端0还是会有距离产生,因为 UIViewController 被延伸了出去。
输入代码后,运行程序:
完美解决,添加的 Text 内容距离顶部的空隙也就不再出现了。