iOS - 使用故事板和自动布局使 UIScrollView 居中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17917437/
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
iOS - Using storyboard and autolayout to center the UIScrollView
提问by Satyam
I'm creating iOS app using story board and auto layout so that it will work good on both iPhone4 and iPhone5. Below is the screen shot of the view that I'm creating using story board.
我正在使用故事板和自动布局创建 iOS 应用程序,以便它在 iPhone4 和 iPhone5 上都能很好地工作。下面是我使用故事板创建的视图的屏幕截图。
In the above image, I want to keep the scroll view in the middle from leading edge of superview and the right table view. I dont want the scroll view to increase its width in iPhone5. I tried different combinations of constraints, but I couldn't achieve it.
在上图中,我想将滚动视图保持在超级视图前缘和右表视图的中间。我不希望滚动视图在 iPhone5 中增加其宽度。我尝试了不同的约束组合,但我无法实现。
Can some suggest me what are all constraints that I've to set for scroll view so that it will be in center.
有人可以建议我为滚动视图设置的所有约束是什么,以便它位于中心。
采纳答案by Fogmeister
You will need to do this by adding an additional view to the screen.
您需要通过向屏幕添加附加视图来完成此操作。
At the moment you have...
目前你有...
- UIView (main view)
|
| - scrollView
| - tableView
You should put the scroll view inside another view like this...
你应该把滚动视图放在另一个像这样的视图中......
- UIView (main view)
|
| - UIView (spacer View)
| | - scrollView
|
| - tableView
Now what you can do is have these constraints...
现在你可以做的是有这些限制......
spacer view leading edge constraint to super view = 0
spacer view trailing edge to table view leading edge = 0
table view width = (whatever the width is)
table view trailing edge to super view = 0
This will lay out the spacer view and the table view so that the spacer view will grow.
这将布置间隔视图和表格视图,以便间隔视图增长。
Now you need to add...
现在你需要添加...
scroll view width = x
scroll view height = y
scroll view centered vertically in super view
scroll view centered horizontally in super view.
Now, because the scroll view's super view is the spacer view then it will always be centered in between the table view and the rest of the space.
现在,因为滚动视图的超级视图是间隔视图,所以它总是在表格视图和其余空间之间居中。
回答by Suragch
How to Center a View on the Storyboard
如何在情节提要上居中视图
This answer has been updated for Xcode 8.
此答案已针对 Xcode 8 更新。
This answer is for people who are just doing a general search for how to center things using a storyboard and don't understand constraints.
这个答案适用于那些只是一般搜索如何使用故事板将事物居中并且不了解约束的人。
I assume you are using a Storyboard (not XIB files) and Auto Layout. This is the default nowadays so if you don't know what that means then don't worry about it. If you want to check, though, you can click Main.storyboard in the Project Navigator and then the File inspector. You can make sure that Use Auto Layout is checked:
我假设您使用的是故事板(不是 XIB 文件)和自动布局。这是当今的默认设置,因此如果您不知道这意味着什么,请不要担心。但是,如果您想检查,您可以单击项目导航器中的 Main.storyboard,然后单击文件检查器。您可以确保选中使用自动布局:
Select your button (or whatever view you want to center) on the storyboard. Then click the align button on the bottom right. Select "Horizontally in Container" and "Vertically in Container". Click "Add 2 Constraints".
在情节提要上选择您的按钮(或您想要居中的任何视图)。然后点击右下角的对齐按钮。选择“水平在容器中”和“垂直在容器中”。单击“添加 2 个约束”。
If it wasn't perfectly centered already you may need to do one more thing. Click the "Update Frames" button that is two to the left of the align button. Your view should now be centered on the storyboard.
如果它还没有完全居中,您可能需要再做一件事。单击对齐按钮左侧两处的“更新帧”按钮。您的视图现在应该以故事板为中心。
And more importantly, now when you run your app it should be centered no matter what device size you are using.
更重要的是,现在当你运行你的应用程序时,无论你使用什么设备尺寸,它都应该居中。
I originally learned this from Creating the user interface – Auto layout. The basic idea is still the same, but the UI arrangement has changed a little in Xcode now.
我最初是从创建用户界面 - 自动布局中学到的。基本思想仍然相同,但现在 Xcode 中的 UI 排列已经发生了一些变化。