Reputation: 125
i want to add buttons dynamically along with the scrollview ,suppose there are like 100 buttons to be added on scrollview it would be humongous to add it on nib file ,so i want to know how to write the code , add buttons dynamically on top of image view on scrollview
Upvotes: 4
Views: 12836
Reputation: 217
You can use below code to add multiple buttons on scrollview:
Step 1:
Delegate "UIScrollViewDelegate" to your Viewcontroller.h
for example:
@interface Viewcontroller : UIViewController<UIScrollViewDelegate>
Step 2:
//create you UIScrollView
UIScrollView *Scroll_View= [[UIScrollView alloc]initWithFrame:CGRectMake(0 ,0 ,self.view.frame.size.width ,self.view.frame.size.height-0)];
Scroll_View.delegate= self;
self.automaticallyAdjustsScrollViewInsets= NO;
Scroll_View.backgroundColor= [UIColor clearColor];
Scroll_View.scrollEnabled= YES;
Scroll_View.userInteractionEnabled= YES;
[Scroll_View setShowsHorizontalScrollIndicator:NO];
[Scroll_View setShowsVerticalScrollIndicator:YES];
[self.view addSubview:Scroll_View];
Step 3:
NSArray *optionsAry= [NSArray arrayWithObjects:@"My Profile & My Orders" ,@"Track Order" ,@"Settings" ,@"Contact Us" ,@"About Us" ,@"Terms & Conditions" ,@"Currency Converter" ,@"System Info" ,@"e-Commerce News App (News Billa)" ,@"Social Media" ,nil];
int y= 20;
for(int i=0; i<optionsAry.count; i++){
UIButton *BTN_For_More= [[UIButton alloc] initWithFrame:CGRectMake(20 ,y , Scroll_View.frame.size.width-40 ,35)];
BTN_For_More.tag= i;
BTN_For_More.backgroundColor= [UIColor whiteColor];
[BTN_For_More addTarget:self action:@selector(BTN_For_More_Action:) forControlEvents:UIControlEventTouchUpInside];
BTN_For_More.titleLabel.text= [NSString stringWithFormat:@"%@",[optionsAry objectAtIndex:i]];
BTN_For_More.titleLabel.textColor= [UIColor colorWithRed:12/255.0 green:104/255.0 blue:172/255.0 alpha:1.0f];
BTN_For_More.titleLabel.textAlignment= NSTextAlignmentCenter;
BTN_For_More.titleLabel.font= [UIFont fontWithName:@"SourceSansPro-Regular" size:15];
//3D effects Start
BTN_For_More.layer.shadowColor= [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5f].CGColor;
BTN_For_More.layer.shadowOffset= CGSizeMake(0 ,0);//2,2
BTN_For_More.layer.shadowRadius= 2.0;
BTN_For_More.layer.shadowOpacity= 1.0;
BTN_For_More.layer.masksToBounds= NO;
//3D effects End
[Scroll_View addSubview:BTN_For_More];
y= BTN_For_More.frame.size.height + y + 20;
}//for loop END //10
Scroll_View.contentSize= CGSizeMake(Scroll_View.frame.size.width ,y);
Step 4:
-(void) BTN_For_More_Action:(NSString *)myString{
NSLog(@"you clicked on button %@", myString);
}
Upvotes: 0
Reputation: 73718
What you need to do is create a loop, create UIButtons
. Setup buttons & add them as subviews to UIScrollView
. Code follows.
NSUInteger i;
int xCoord=0;
int yCoord=0;
int buttonWidth=100;
int buttonHeight=50;
int buffer = 10;
for (i = 1; i <= 100; i++)
{
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:aButton];
yCoord += buttonHeight + buffer;
}
[scrollView setContentSize:CGSizeMake(700, yCoord)];
What i am basically doing here is having variables for X & Y co-ordinates. As I am looping through to create UIButtons
am creating the appropriate CGRect
struct to decide where to place the button in the UIScrollView
. After adding that button to scrollView, change the X & Y values to where ever you want to place the next button.
At the end dont forget to set the ContentSize
for scrollview so that it enables scrolling.
PS: All this code is typed free hand, might have small syntactical errors but the logic is solid.
Upvotes: 7