Designing inside a scrollview in xcode 4.2 with st

2019-01-06 13:31发布

I have a vertically scrolling uiscrollview - imagine an 'about this app' page of a tab bar app which goes on a bit and requires a scrollview. It only contains a few images, a video and some text (only the video has been coded in - the rest have been placed in the GUI). In storyboard (Interface Builder?) Xcode 4.2, everything is set up as it should be and works fine, but the view is only as large as what you see on the screen, is it not possible to manually arrange in storyboard the items that are initially offscreen - that you need to scroll up to? The only way I've found so far is to design them on the visible view then navigate them down with the arrow keys..

9条回答
相关推荐>>
2楼-- · 2019-01-06 13:50

I'm currently developing an app for iOS 7, and I did exactly as @Dickey Singh's answer, but it doesn't work in the beginning. After checking the storyboard, I found that we also need to add Auto Layout Constraints for the view controller who holds the scrollView. It seems that such auto layout constraints would be added automatically before Xcode 5, but now we need to do it ourselves. The way to add constraints: First select the view controller in the storyboard; Enter 'Editor' in the top menu; Select the 'Resolve Auto Layout Issues'; Select the 'Add Missing Constraints In Container'. Done :-)

查看更多
Explosion°爆炸
3楼-- · 2019-01-06 13:56

Just change the 'Simulated Size' of the view controller to freeform and set a height that is larger than the usual size, you will be able to see all the outlets you need to edit.

查看更多
Bombasti
4楼-- · 2019-01-06 13:56

On iOS 6.0 you can drag a Container View inside your Scroll View. This will automatically create a new View for your content, outside of the current scene. You can then resize this view as big as needed to fit your content.

I believe you would still have to set the ScrollView content height at runtime, but at least you can design you content view at once without having to scroll up and down on IB.

查看更多
Explosion°爆炸
5楼-- · 2019-01-06 13:56

I've been struggling with this for a while now, and every single thing I've tried has failed.

Specifically, What I am trying to achieve is a freeform sized modal dialog with a scrollable view containing a container for another view. I have had a lot of varied results, including occasionally having it working correctly. Most often I get it looking exactly correct, but with no scrolling.

In finally downloaded Dickey Singh's code, which worked perfectly but had nothing special. (Excellent clean solution BTW). So, I added a container view to it, exactly as I had in my code, and it broke!

After some experimenting, I worked out what is going on. Just bear with me.

1) Using Auto Layout, the size of the scroll view seems to dictate what the scrolling bounds will be. Setting "contentSize" in "USer Defined Runtime Attributes" seems to have no effect on this, and neither does setting "contentSize" or "bounds" in "viewWillDisplay" or "viewDidLoad". Thus if the initial size of the scroll view is 800x800, that will be all the space that can be displayed. For this reason, when I want a scrollable region, I create a container view and then put the scrolling view inside the content.

2) Without Auto Layout, setting "contentSize" in "User Defined Runtime Attributes" works, as does by setting it programmatically in "viewDidLoad". I prefer to use "User Defined Runtime Attributes" because it keeps the size with the layout. This solution allows you to use scrolling view with more flexibility, since it can be any size at design time.

3) Regardless of Auto Layout, if any view within the scrolling region exactly matches EITHER the horizontal or vertical frame bounds, then the scroll view ceases to function as a scroll view. This applies to my own code and to Dickey Singh's code in every possible configuration that I have tried.

I have no idea what is causing (3), but it is clearly a bug.

I hope this helps everybody out there who is struggling to use scroll view. I imagine that some people are using them without any problem, and some (like me) have had noting but problems with them.

查看更多
对你真心纯属浪费
6楼-- · 2019-01-06 13:58

New: 3/26/2013

I stumbled upon what I think is even simpler way of dealing with UIScrollView directly in storyboard.

  1. No code needed, just storyboard settings. This maybe new in iOS6.1 / Xcode 4.6
  2. No need to disable constraints (i.e. uncheck "Use autolayout" in File Inspector for storyboard file)
  3. No need to add UIScrollView* scrollView; in .h
  4. No need to add self.scrollView.contentSize = ... in overrides of viewWillAppear or viewDidLoad

Here is what I did (important parts highlighted with **): (see code)

  1. Create a new project with storyboard enabled
  2. Drop in a UIScrollView, set class in identity inspector for view controller
  3. In attributes Inspector, change Size under simulated metrics to Freeform**
  4. Select scroll View; In attributes inspector, turn on "scroll enabled" and "background" to "White" (you'll figure out why - if you don't)
  5. Under Size Inspector (with scroll view selected) change the height to 900 for example**
  6. Add buttons, one on top and one at the bottom
  7. Add a default handler for buttonTouchUpInside for both buttons and simply Log sender.

See Code Select the View Controller and scroll view and check inspectors.

查看更多
闹够了就滚
7楼-- · 2019-01-06 14:00

I feel your pain. The only way I found is to manually pan the scroll view in the size inspector to reveal the portion of the view that you wish to visually edit.

Use a UIView to contain elements so they are positioned relatively to this view. Add the view as a subview to the scrollview at 0,0.

  1. pan: use the Y coordinate say to -200, then edit the contents.
  2. to place more contents in the hidden part, pan again to reveal new real-estate
  3. when finished, restore the values of the ScrollView's height and X,Y position.

Make sure the scroll view frame rectangle is smaller than the contained view.

查看更多
登录 后发表回答