0

I have a really long ViewController filled with view controls (that I placed). There isn't enough space to show all the labels, text fields, etc at once, so I would like to use a UIScrollView (or something similar) so the user could just scroll down the page and see the rest of the controls. However, I tried dragging a UIScrollview ontop of a ViewController and then tried to drag but I was not able to drag the screen at all. What can I do to enable this "scrolling" functionality. This isn't a TableView - just a regular ViewController.

aerain
  • 1,149
  • 3
  • 12
  • 17

6 Answers6

1

I am not sure if your question is whether you expect to scroll within XCode (you can't) or whenever you execute the code.

In order to scroll the view when you run the app, the content of the scrolling view needs to be larger than its frame. Therefore, assuming the frame is full screen on iPhone (0,0,320,460), if you were to write:

[self.scrollView setContentSize:CGSizeMake(400, 460)];

Then the scrolling view would be scrollable horizontally to display the remaining 80 pixels.

JP Hribovsek
  • 6,707
  • 2
  • 21
  • 26
1

Create your uibutton in the usual way, linking it in the usual way. (creating outlets and actions int the .h file is what I am currently doing), then just tell the computer where you actually want it to be using:

myButton.frame = CGRectMake(20, 800, 90, 30); //in viewDidLoad is where I put it

check out this thread: How to change the position of a UIButton dynamically

Note:
This doesn't work if you make the myButton a subview of a view other than the scrollView. At least in my case, this didn't work. the button could be seen, but clicking on it didn't activate the associated IBAction.

This works:

[scrollView addSubview:myButton];

although it doesn't seem necessary.

Community
  • 1
  • 1
juggler
  • 319
  • 1
  • 5
  • 16
0

You need to set the scrollview on the bottom and then paste all the viewcontrollers ontop of that.

tracifycray
  • 1,423
  • 4
  • 18
  • 29
0

First drag a UIScrollView into your current view

Then start copy pasting all the views inside it, form the original view

Please check this answer Are there any good UIScrollView Tutorials on the net?

Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0

I believe you are using Interface Builder / storyboard to layout your content. Follow these steps:

  1. Layout your UIScrollView in IB like you did. Set its width and height in Size Inspector to, let's say, 320 by 460.
  2. Connect the scrollview to your .h file as IBOutlet. I will name it scrollView.
  3. Set the contentSize of the scrollview to be larger than its frame. For example

    self.scrollView.contentSize = CGSizeMake(320.0f, 1000.0f);
    
  4. Add a UIView to scrollView in IB. The size of the UIView should be the same as the scrollview's contentSize.

  5. When laying out your content on the UIView, set Y of UIView in Size Inspector to -400 (or any value you like to offset) and you will be able to layout more elements in the UIView.
Rick
  • 1,818
  • 1
  • 15
  • 18
0

here's a better way of doing it:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];

I found this code here: How do I create a basic UIButton programmatically?, posted by mahboudz

this is essentially the same as what I put above, but does it all programatically, which comes with the bonus that you should (haven't tried yet), be able to create multiple such buttons using for loops, for example.

Community
  • 1
  • 1
juggler
  • 319
  • 1
  • 5
  • 16
  • multiple buttons using loops: http://stackoverflow.com/questions/2646297/add-a-multiple-buttons-to-a-view-programatically-call-the-same-method-determin – juggler Mar 24 '13 at 18:58