xcode 带有导航栏+自动布局的滚动视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34663956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scrollview with navigation bar + auto layout
提问by bpiec
I have a ViewController embedded in NavigationController. I placed ScrollView inside it, then content view covering the full area (to set ScrollView's content size) and some labels. I tried to set constraints for all of the controls but I failed.
我在 NavigationController 中嵌入了一个 ViewController。我将 ScrollView 放在其中,然后内容视图覆盖整个区域(以设置 ScrollView 的内容大小)和一些标签。我试图为所有控件设置约束,但失败了。
This is how it looks in Xcode:
这是它在 Xcode 中的样子:
And this is in Simulator:
这是在模拟器中:
Red color is scroll view, green – content view. Of course I can scroll the view to get the bottom label on screen.
红色是滚动视图,绿色是内容视图。当然,我可以滚动视图以获取屏幕上的底部标签。
What I did wrong? What are the proper constraints? I want of course the ScrollView to cover the entire usable area of window (from bottom of navigation bar to bottom of screen).
我做错了什么?什么是适当的约束?我当然希望 ScrollView 覆盖窗口的整个可用区域(从导航栏底部到屏幕底部)。
For full reference you can download the project.
如需完整参考,您可以下载该项目。
回答by Roshan lamichhane
Swift 3:
斯威夫特 3:
self.automaticallyAdjustsScrollViewInsets = false
回答by Teja Nandamuri
I think the gap you see is caused by the navigation bar. The gap is off size 64 which is the size of the navigation bar + status bar (44+20). You need to constraint the scroll bar to the top layout guide.By pushing this 64 negatively. So your scrollview top constriant will be -64.
我认为您看到的差距是由导航栏引起的。差距超过 64 号,即导航栏 + 状态栏 (44+20) 的大小。您需要将滚动条约束到顶部布局指南。通过将这个 64 推向负面。所以你的滚动视图顶部约束将是 -64。
This can also be done in one click, by clicking on the position viewbutton in size inspectorin xcode.
这也可以通过单击xcode中大小检查器中的位置视图按钮一键完成。
Click on the position view and select fill container vertically.This will constraint the scrollview to top layout guide. Now select the scrollview and click on the update constraints in the storyboard.
单击位置视图并垂直选择填充容器。这会将滚动视图约束到顶部布局指南。现在选择滚动视图并单击故事板中的更新约束。
As you can see, the contentView is not constrianed to cover the view just like scrollview , there is space at the button of the content view, when the view loads, the navigation bar will occupy the additional space, and the contentview will be exactly at where you need.
如您所见, contentView 并没有像 scrollview 一样被约束覆盖视图,内容视图的按钮处有空间,当视图加载时,导航栏会占用额外的空间,而内容视图将正好位于你需要的地方。
回答by Nik Kov
Here's nice tutorial about scrollView and Auto Layout. I spend a lot of time with same kind of problem until found this tutorial. http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/.
这是关于滚动视图和自动布局的好教程。在找到本教程之前,我花了很多时间解决同样的问题。http://spin.atomicobject.com/2014/03/05/uiscrollview-autolayout-ios/。
The way is to drag scrollview and content view to all of the area like if there wouldn't present a navigation bar. Also make right constraints.
方法是将滚动视图和内容视图拖动到所有区域,就像没有导航栏一样。还要做出正确的约束。
回答by Jules
The swift 3 code works but the interface builder is trying to compensate for the navigation bar so when adding view ignore the bar and place the view over it. The IB will compensate.
swift 3 代码有效,但界面构建器正在尝试补偿导航栏,因此在添加视图时忽略该栏并将视图放在其上。IB 会补偿。