iOS Storyboard View Controller约束无法正常工作

时间:2016-03-02 10:23:55

标签: ios iphone uiviewcontroller uistoryboard nslayoutconstraint

我在Storyboard中有我的ViewController。在Storyboard中一切似乎都很好,但是当我在模拟器上构建它时,它会在侧面显示奇怪的白色位置。当我将视图的背景更改为蓝色时,我意识到白色空间是由我的视图引起的。什么会导致这种问题?

我的wiews和约束的层次结构: enter image description here

我的故事板视图: enter image description here

在模拟器上查看: enter image description here

1 个答案:

答案 0 :(得分:0)

Make sure your scrollViews and contentViews LeadingSpace and TrailingSpace are 0, It seems you are leaving 20 pts from LeadingSpace and TrailingSpace.

enter image description here

Your constraints should look like this:

Make sure you set MainView/ContentView width equals to scrollView.

enter image description here

What you are messing is, you are setting scrollView width equal to ViewControllers View, remove that constraint and it should work hopefully.

Edit/Update:

I have done demo for test and it is working perfectly fine without any workaround. You can see below screen shot of Storyboard Preview for iPhone 5S and iPhone 6S Plus. ViewControllers view is in Blue color and ContainerView is in Orange color.

enter image description here

Here are the constraint of scrollView: (While adding constraint make sure you press Alt button)

enter image description here

And the ScrollViews constraint are as below:

enter image description here

ContainerView Contraints:

enter image description here

And the ContainerViews constraint are as below:

enter image description here

One last thing, inside container view all view's should have top to bottom constraint in order to calculate the height required for scrollView. And also do not create conflicting constraints as - Setting imageView's LeadingSpace and TrailingSpace to ContainerView(SuperView) and at the same time setting fix width.