2

Possible Duplicate:
steps for creating UIScrollView with Interface Builder

I am using UIScrollView for one of my view. I have added view first and then scroll view, then some controls over scroll view, all are added through IB. Every things works fine until the page is fit, some controls need to be added down that is exceeding the UIView length, I am not sure, how to add controls in IB in that case, So I have added programmatically, but my scrollview is not scrolling down, it is scrolling upto my view height, I have tried setting the new view height, scrollview's frame and content size nothing works, Can anyone please suggest me what to do now...

i) how to add control in IB more than its view length.(view height are disabled).

ii) How to make my scrollview which covers full view.

Community
  • 1
  • 1
Newbee
  • 3,231
  • 7
  • 42
  • 74
  • 2
    Take a look at my answers [here](http://stackoverflow.com/questions/9118796/steps-for-creating-uiscrollview-with-interface-builder/9119126#9119126) and [here](http://stackoverflow.com/questions/9987436/how-to-design-the-content-of-a-uiscrollview-in-a-nib-easily/9987787#9987787). – rob mayoff Oct 22 '12 at 05:51

3 Answers3

3

Create the view for the UIScrollView separately.

Technically, UIScrollView can hold controls directly in it. But that is not the point of it. You should always (if using IB) design the view separately and then place it inside the UIScrollView and set the contentSize property to the size of that view.

So, instead of putting contents directly in the UIScrollView in the main view, drag a separate view from the IB objects library on the nib (not on the main view).

Set the width of this view to the width of UIScrollView. Start placing controls on it and extend the height of view. For every new control to be placed, the height must be increased so as to hold that control. You should not place control outside a view from IB. (Although you can).

After designing the view, drag the view inside the UIScrollView and set the Y value of this view to 0. (When you drag a view inside UIScrollView it seems to upset it's Y value).

Now programmatically set the contentSize of UIScrollView to the size of this view. For this you'll need to create an IBOutlet for the UIView you just created.

myscrollview.contentSize = myview.frame.size;

And you're done.

-EDIT-

Revealing XIB Controls

atastrophic
  • 3,153
  • 3
  • 31
  • 50
  • I got what are you saying, but one question, Instead of adding a new view I have added on scroll view, I can create a new view as you said, how do I move all the controls which is placed in scroll view to new view, is there any way like copy past, sorry if it sounds crazy, bcoz I have added more than 30 controls, I have to position again otherwise ... – Newbee Oct 22 '12 at 05:52
  • it's easy. there on the view designer, you have a little left arrow at the bottom left which reveals all the elements in the `XIB` file. from there navigate to your `UIScrollView`, expand and drag all elements into your new view – atastrophic Oct 22 '12 at 05:57
  • check the edited answer for a screenshot – atastrophic Oct 22 '12 at 06:01
1

it is so simple. Follow the steps I'm giving you below you will get what you want.

decide the actual size that is the size in which you want your scroll view to display. say it is 100 * 5o.

  • set your UIView size also 100 * 50.

  • Now increase the scrollview hieght in nib.

  • add what you want

  • Now check upto what size you have reached.

  • Suppose it is reached upto 300

  • So set your contentsize 100 * 300 or 100 * 310 programmatically

  • In nib set scrollview size 100 * 50

Enjoy programming.

Niru Mukund Shah
  • 4,637
  • 2
  • 20
  • 34
  • content size should match view's height ? – Newbee Oct 22 '12 at 05:37
  • no it should match with the size of the content length – Niru Mukund Shah Oct 22 '12 at 05:38
  • 1
    that's why I've mentioned steps in this answer.Increase the height of your scrollview. Add what you want. then note down the height of the scrollview from nib itself. Then reduce the size of your scrollview. Don't worry if you are not able to see your added content. Then add the content size programmatically. that is width you want . & height that you have noted from nib. – Niru Mukund Shah Oct 22 '12 at 05:44
1

It's very simple, first remember your UIScrollView Frame and then just change UIScrollView frame's Y-position by subtracting 100 (e.g 0 to -100) and add same to height(460 to 560). Then simply add you controls at your required position by continuing this steps...

For eg:

ScrollView Frame (0,0,320,460)

after CHANGE:
(0,-100,320,560)

After finishing with controls adding simply set previous frame like: (0,0,320,460)

Loquatious
  • 1,791
  • 12
  • 21