Putting a UIScrollview in a viewcontroller (storyboard)

China☆狼群 提交于 2019-12-24 13:09:04

问题


Hey I want to add a scroll view to my view controller, I have dragged a UIScrollView onto the canvas, it is the required size (228*128). I want this scrollview to scroll a view of size (576*128), i.e. double width. I'm not sure how to go about doing this. Do I first draw the (576*128) view on a separate xib file? How would I link all this up after? The image below is my setup. Do I have to create a custom class for the UIView that contains the content and init this is my view controller? Just not sure how to go about it. Thanks!

updated image below.........


回答1:


You can lay this out entirely in Interface Builder.

  1. Start with a fresh ViewController. In the Size Inspector on the right, set the Simulated Size to Freeform. Set width to 640 and height to 600. This will give you a sufficiently wide ViewController to see the full width of your scroll view (for layout purposes only).

  2. Drag out a scrollView. Add constraints to center it in the view, and constrain it to the bottom of your ViewController. Constrain its width to 576 and its height to 128. We'll fix up the width in a later step.

  3. Add a contentView to the scrollView by dragging out a UIView and dropping it into the scrollView. Pin it to the left, top, right, and bottom of the scroll view and constrain its width to 576 and height to 128. To do this, click on the pin icon at the bottom of the screen |-[]-|, uncheck Constrain to margins, turn on all four orange I-beams (struts), set their constants to zero, check the boxes next to width and height and set their values to 576 and 128 respectively. Finally, click on Add 6 constraints.

  4. Make the background of the contentView yellow so you can see it.

  5. Add content to your contentView. I added three labels "Left Side", "Middle", and "Right Side".

  6. Now let's make the scrollView the right size. Click on the scrollView's width constraint and make it a Placeholder by clicking on the Remove at build time checkbox.

  7. Add another width constraint to the scrollView. Set it equal to 228, and set its priority to 750. With this lower priority, you won't have conflicts in Interface Builder, but when you build the other one will be left out and this will constrain your scrollView to a width of 228.

  8. At this point, your Document Outline will look like this showing all of the constraints:

  9. Now build, and your scrollView will scroll!




回答2:


Link your scrollview to an outlet, then do this:

Objective-C:

[scrollView setContentSize:CGSizeMake(576, 128)];

Swift:

scrollView.contentSize = CGSize(width:576, height: 128)

After this, you can add elements to your scrollview seperately, or you could add them to a uiview and then add it to your scrollview by doing:

Objective-C:

[scrollView addSubview:view];

Swift:

scrollView.addSubview(view);

(By the way, you say 'double width'. 228 * 2 is not 576px but 456px)



来源:https://stackoverflow.com/questions/27765750/putting-a-uiscrollview-in-a-viewcontroller-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!