問題描述
我正在使用故事板和自動布局創(chuàng)建 iOS 應(yīng)用程序,以便它在 iPhone4 和 iPhone5 上都能正常工作.下面是我使用故事板創(chuàng)建的視圖的屏幕截圖.
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.
在上圖中,我想將滾動視圖保持在超級視圖的前沿和右側(cè)表格視圖的中間.我不希望滾動視圖在 iPhone5 中增加其寬度.我嘗試了不同的約束組合,但我無法實現(xiàn).
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.
有人可以建議我為滾動視圖設(shè)置哪些約束,以便它位于中心.
Can some suggest me what are all constraints that I've to set for scroll view so that it will be in center.
推薦答案
您需要通過在屏幕上添加一個額外的視圖來做到這一點.
You will need to do this by adding an additional view to the screen.
目前你有...
- UIView (main view)
|
| - scrollView
| - tableView
你應(yīng)該把滾動視圖放在另一個像這樣的視圖中......
You should put the scroll view inside another view like this...
- UIView (main view)
|
| - UIView (spacer View)
| | - scrollView
|
| - tableView
現(xiàn)在你可以做的是有這些限制......
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
這將布置 spacer view 和 table view 以便 spacer view 增長.
This will lay out the spacer view and the table view so that the spacer view will grow.
現(xiàn)在你需要添加...
scroll view width = x
scroll view height = y
scroll view centered vertically in super view
scroll view centered horizontally in super view.
現(xiàn)在,因為滾動視圖的超級視圖是間隔視圖,所以它將始終居中在表格視圖和其余空間之間.
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.
這篇關(guān)于iOS - 使用故事板和自動布局使 UIScrollView 居中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!