Samrat Mazumdar
Samrat Mazumdar

Reputation: 2681

How to setup a scrollview with many buttons in it?

I have been trying to add buttons on scroll view, but unable to do them. I want to add more 7 buttons in a view, but I want that user can get access to all seven buttons by scrolling down. How can I do that?

Please someone guide me or else provide me a link where I can understand.

EDIT:

I have created a view based application and named as poo1 & in the poo1ViewController.h,

@interface poo1ViewController  : UIViewController <infodelegate>
{
 IBOutlet UIButton *a;
 IBOutlet UIButton *b;
 IBOutlet UIButton *c;
 IBOutlet UIButton *d;
 IBOutlet UIButton *e;
 IBOutlet UIScrollView *fView;
}
@property (nonatomic,retain) UIScrollView *fView;;
@property (nonatomic,retain) UIButton *a;
@property (nonatomic,retain) UIButton *b;
@property (nonatomic,retain) UIButton *c;
@property (nonatomic,retain) UIButton *d;
@property (nonatomic,retain) UIButton *e;
-(IBAction) ia:(id)sender;

And in implementation file

#import "poo1ViewController.h"


@implementation poo1ViewController

@synthesize a;
@synthesize b;
@synthesize c;
@synthesize d;
@synthesize e;
@synthesize fView;
-(IBAction) ia:(id)sender {
}


@end

LIke this I have a method for all the button, now I want all the button to be present when the App opens but to access all the user needs to scroll down, I don't know how to implement the scrollview in the present view, so the poo1ViewController.xib view gets to be scrollable.

Upvotes: 0

Views: 2423

Answers (2)

Andrew
Andrew

Reputation: 2710

The UIScrollView won't be scrollable in Interface Builder. You'll either have to stretch it out and add your UIButtons where you want them to be and then resize the UIScrollView at the end, OR you'll need to add your UIButtons programmatically to your UIScrollView.

Personally, I'd opt for doing this one programmatically. You can either create the UIButtons programmatically as well (what I'd opt to do), or if you want to cut corners, you can just add them to your UIScrollView in your xib file and make sure you have an IBOutlet connecting them to your code. Then in your code set their frames to where you want them.

-(void)viewWillAppear:(BOOL)animated {
    // CGRectMake(x,y,width,height)
    a.frame = CGRectMake(20, 20, a.frame.size.width, a.frame.size.height);    
    b.frame = CGRectMake(20, 50, b.frame.size.width, b.frame.size.height);

    // etc. etc. for the rest of your buttons

Then make sure you set the frame and contentSize of your UIScrollView otherwise it won't scroll.

  myScrollView.frame = self.view.frame;
  myScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 
                                        myScrollView.frame.size.height
                 + how many pixels you want it to be able to scroll up and down>;
}

Upvotes: 0

Alex Cio
Alex Cio

Reputation: 6052

I had the same problem too. I created buttons programmatically and couldn't access all of them because the view didn't scroll. Important for the scrolling is the last line of my code. First i set a specific starting point "positionX" for the buttons. After that i loop through my database and create a button for each entry. After creating a new button, i increment positionX by the height of a button + 10. When all buttons have been created, the loop ends, and the value of positionX will be also used for the height of my UIScrollView.

int positionX = 10;

for (int i = 0; i < [activities count]; i++) {

    //get single object out of nsarray
    Activity *activity = [activities objectAtIndex:i];

    //create button for an activity
    UIButton *activityButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [activityButton setTitle:activity.definition forState:UIControlStateNormal];
    activityButton.frame = CGRectMake(10, positionX, 300, 40);
    [self.activityScrollView addSubview:activityButton];

    positionX += 50;
}

//set the content size of the scroll view
[self.activityScrollView setContentSize:CGSizeMake(320, positionX + 20)];

Maybe this website is useful too. I solved the problem after reading the tutorial.

Upvotes: 1

Related Questions