how to set different image for dynamic buttons in ios? - ios

I am creating 20 buttons dynamically in my apps project now i want to set images for each button like not same image will be different for each buttons. I try lot of solution but not getting proper result. I am implemented following code for this......
int yPossion = 150, xPossion = 44; int temp = 0;
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
[self.view addSubview:scrollView];
for (int i = 0; i < [objectName count]; i++) {
SMSCat1 *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
//Label Dynamic Code
UILabel *label = [[UILabel alloc] init];
[label setText:cat.Name];
[label setTextColor:[UIColor brownColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(5, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:#"icon-menu.png"]
forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:#"%d", i] forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted = YES;
[scrollView addSubview:aButton];
xPossion += aButton.frame.size.width + 35;
temp++;
if (temp == 3) {
yPossion = aButton.frame.origin.y + aButton.frame.size.height + 20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width - 15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, yPossion - 50)];
}
}
}
Please help me to complete that task. I added 20 images on image folder but how to set that image for each buttons its like (can i use settag for this).

Don't be afraid to ask if you are new. Looking at other peoples code and learning is how we all get better at what we do. You can find the answer to your question here - how to set auto layout for dynamically created buttons? Hope this helps you

Related

How to add Dynamic elements in the reverse direction to a Uiscrollview in IOS?

I had a scrollview in my application with dynamic elements which i am adding from right to left like this
if(array.count>0)
{
[[cell.contentView viewWithTag:777] removeFromSuperview];
float x=315.0;
float width=0.0;
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(70, 52,245,20)];
scroll.backgroundColor=[UIColor clearColor];
scroll.tag=777;
[scroll setContentOffset:CGPointMake(70,245)];
scroll.showsHorizontalScrollIndicator=NO;
for(int i =0; i <[array count] ; i++)
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundColor:[UIColor colorWithRed:133.0/255.0 green:155.0/255.0 blue:179.0/255.0 alpha:1.0]];
btn.layer.cornerRadius = 2;
[btn setTitle:[[array objectAtIndex:i] objectForKey:#“ggg”] forState:UIControlStateNormal];
btn.userInteractionEnabled=NO;
[btn.titleLabel setFont:[UIFont fontWithName:#"Helvetica Neue" size:10.0]];
CGSize size = [btn.titleLabel.text sizeWithAttributes:
#{NSFontAttributeName:
[UIFont fontWithName:#"Helvetica Neue" size:10.0]}];
x=x-(size.width+10);
//or whatever font you're using
[btn setFrame:CGRectMake(x,0,size.width+10,15)];
[scroll addSubview:btn];
width=width+x;
x=x-5;
}
scroll.contentSize=CGSizeMake((width+50),20);
// [scroll setPagingEnabled:YES];
[cell.contentView addSubview:scroll];
}
here everything working very fine,but i am not able to scroll to the last element added at the left.scroll was only allowing me to scroll up to 70 from left to right .Can anybody tell me where i am going wrong on this?
-(void)addButtonInScrollViewFromArray:(NSArray *)array{
NSInteger arrayCount=array.count;
NSInteger numberIteration=0;
CGFloat buttonXValue=300;
UIScrollView *buttonScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, 360, 60)];
while (numberIteration<arrayCount) {
UIButton *buttonCreated=[UIButton buttonWithType:UIButtonTypeCustom];
buttonCreated.frame=CGRectMake(buttonXValue,5, 50, 50);
buttonXValue-=60;
[buttonCreated setTitle:[NSString stringWithFormat:#"Button%d",numberIteration] forState:UIControlStateNormal];
[buttonScrollView addSubview:buttonCreated];
numberIteration++;
}
CGFloat totalLength=numberIteration*60;
buttonScrollView.contentSize=CGSizeMake(totalLength,60);
}
this function you can pass the array
Can you try to below one
scroll.contentSize = CGSizeMake(x+size.width+10,20);

how to create action multiple dynamic buttons in ios?

i am creating multiple buttons dynamically now i want to set action for each buttons.
when i press buttons its show result on next screen in my application .
i am using FMDB library to read data from database.(read database according to id on buttons press). i know how to read database from database . i want only fetch data on buttons press and display that
data on table view .how to create action for each buttons?
this my buttons code :
-(void)DynamicButton:(NSMutableArray*)objectName
{
for(UIView *view in [scrollView subviews])
{
[view removeFromSuperview];
}
int yPossion = 100, xPossion = 44; int temp = 0;
for (int i = 0; i<[objectName count]; i++)
{
SMSCategory *cat = [objectName objectAtIndex:i];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTag:i];
[aButton setTranslatesAutoresizingMaskIntoConstraints:YES];
[aButton setBackgroundColor:[UIColor blackColor]];
[aButton setBackgroundImage:[UIImage imageNamed:#"icon-menu.png"]
forState:UIControlStateNormal];
[aButton setTitle:[NSString stringWithFormat:#"%d",i]
forState:UIControlStateNormal];
[aButton setFrame:CGRectMake(xPossion, yPossion, 70, 60)];
aButton.highlighted=YES;
[scrollView addSubview:aButton];
;
xPossion += aButton.frame.size.width+35;
temp++;
if (temp==3)
{
yPossion = aButton.frame.origin.y+aButton.frame.size.height+20;
temp = 0;
xPossion = 44;
yPossion += aButton.frame.size.width-15;
[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width ,yPossion-
50)];
}
UILabel *label = [[UILabel alloc] init];
[label setTranslatesAutoresizingMaskIntoConstraints:YES];
[label setText:cat.Name];
[label setTextColor:[UIColor blackColor]];
label.font = [UIFont systemFontOfSize:12];
[label sizeToFit];
[label setFrame:CGRectMake(4, 44, 70, 60)];
[scrollView addSubview:label];
[aButton addSubview:label];
}
}
UIButton *test = [self buttonWithTitle:#"test" target:self selector:#selector(testDoit) frame:CGRectMake(self.view.center.x-50, 270.0, 100.0, 50.0) : 12];
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame :(int)fontSize{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:frame];
[button setTitle:title forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:fontSize];
button.userInteractionEnabled = YES;
button.titleLabel.textAlignment = UITextAlignmentCenter;
button.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
button.titleLabel.shadowOffset = CGSizeMake (1.0, 0.0);
button.titleLabel.textColor = [UIColor blackColor];
button.showsTouchWhenHighlighted = YES;
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleRightMargin|
UIViewAutoresizingFlexibleTopMargin|
UIViewAutoresizingFlexibleHeight|
UIViewAutoresizingFlexibleBottomMargin;
[button addTarget:self action:inSelector forControlEvents:UIControlEventTouchUpInside];
//[button autorelease];
return button;
}

UIScrollView needs to get bigger every time I add a button

The scrollView is created in my storyboard and is horizontal. Here is the method where I add a button for every loop and need to increase the size of the scrollView every time a button is added.
- (void) populateGroups {
NSInteger x = [self.groupNameArray count];
for (int i = 0; i < x; i++) {
NSString *name = [self.groupNameArray objectAtIndex:i];
// Create Button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(count, 0, 100, 54)];
[button setTitle:name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.groupScrollView addSubview:button];
count = count + 100;
// Increase ScrollViewSize
self.groupScrollView.frame = CGRectMake(0, 0, frameWidth, 80);
frameWidth = frameWidth + 100;
}
}
The scrollView stays the same when I try to add two buttons to the view.
you have to increase the content size of the scroll view like:
self.groupScrollView.contentSize = CGSizeMake(frameWidth, 80);
and better to do it after the loop.

IOS UIButton Target doesnot working

here is my code
for (NSString *message in self.messages) {
label = [[UIButton alloc] initWithFrame:CGRectZero];
[label setTitle:message forState:UIControlStateNormal];
//label.titleLabel.text = message;
label.tag = i;
[label addTarget:self action:#selector(gototest:) forControlEvents:UIControlEventTouchDown];
CGSize size = [message sizeWithFont:label.titleLabel.font];
CGFloat width = size.width + kPADDING;
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
[self addSubview:label];
i++;
xPos += width;
}
-(void)gototest:(UIButton*)sender
{
int tag = sender.tag;
NSLog(#"test %#",#"ccc ");
}
So nothing happen when I click on the buttons.
Note that the class is extend from UIScrollView and also I have to create a marquee with different news and when I click in any of this news, I have to go to the detail of the chosen new.
Please help me
UIControlEventTouchDown should not be the problem, it works too. Can you try the following
label = [UIButton buttonWithType:UIButtonTypeCustom];
instead of
label = [[UIButton alloc] initWithFrame:CGRectZero];
Too I suspect the following line too
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
the frame you are setting confirm it is correct, in normal usage it may appear that your UIButton is visible, but it may not. Try put
label.clipsToBounds=YES;
and check, if your UIButton is actually visible, that might be the reason your action not working..
UPDATE
Ok, As per my understanding, as you are saying, you are trying to create Marquee, and the original implementation had UILabel and you are trying to change that to UIButton. Let change few things as the code below,
for (NSString *message in self.messages) {
label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = message;
label.tag = i;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(gototest:)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[label addGestureRecognizer:tapGestureRecognizer];
label.userInteractionEnabled = YES;
CGSize size = [message sizeWithFont:label.font];
CGFloat width = size.width + kPADDING;
label.frame = CGRectMake(xPos, 0.0, width, self.frame.size.height);
[self addSubview:label];
i++;
xPos += width;
}
And your action
-(void)gototest:(UITapGestureRecognizer*)sender
{
int tag = sender.view.tag;
NSLog(#"test %#",#"ccc ");
}
Use touch event "UIControlEventTouchUpInside" intends of "UIControlEventTouchDown".
It will works fine :
[label addTarget:self action:#selector(grottiest:)
forControlEvents:UIControlEventTouchUpInside];
Edit :
label = [[UIButton alloc] initWithFrame:CGRectMake(xPos, 0.0, 100, 100)];
[label setTitle:message forState:UIControlStateNormal];
//label.titleLabel.text = message;
label.tag = i;
[label addTarget:self action:#selector(gototest:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:label];
Your code is working fine with single button. The problem is somewhere else..
UIButton *label = [[UIButton alloc] initWithFrame:CGRectZero];
[label setTitle:#"Click" forState:UIControlStateNormal];
//label.titleLabel.text = message;
label.tag = 0;
[label addTarget:self action:#selector(gototest:) forControlEvents:UIControlEventTouchDown];
label.frame = CGRectMake(0,0,100,50);
[self addSubview:label];
Replace:
label = [[UIButton alloc] initWithFrame:CGRectZero];
with
UIButton *label = [[UIButton alloc] initWithFrame:CGRectZero];

UIScrollView not scrolling iOS

I am trying to create several UIScrollViews in a single view that scroll horizontally in iOS. This is my code so far:
-(void)updateSection {
[feedLoadingActInd stopAnimating];
feedLoadingActInd.hidden = YES;
builder = [NSMutableArray array];
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
for (int i = 0; i < itemArticleArray.count; i++) {
testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosBut, 40, 40, 40)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosBut += testButton.frame.size.width;
NSLog(#"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += 2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 350, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition += 350;
[seperatorView addSubview:testButton];
[scrollView addSubview:seperatorView];
[builder addObject:testButton];
}
[self addSubview:scrollView];
[scrollView setUserInteractionEnabled:YES];
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
NSLog(#"scroll.contentsize.width = %f", scrollView.contentSize.width);
}
However, none of the scroll views are actually scrolling, which I am confused about, as there are multiple buttons being added. Also, the buttons that I have added, are not actually doing anything when I press them. They should be running the buttonPressed method, and it doesn't?
Any help would be muchly appreciated!
try this,
float xPosition = 0;
float xPosBut = 0;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100,self.view.frame.size.width, self.view.frame.size.height - 29)];
[scrollView setScrollEnabled:YES];
scrollView.backgroundColor = [UIColor yellowColor];
for (int i = 0; i < 10; i++) {
UIButton *testButton = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 10, scrollView.frame.size.width, 50)];
[testButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[testButton setTitle:#"Test" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
testButton.backgroundColor = [UIColor blueColor];
xPosition += testButton.frame.size.width;
NSLog(#"xposbut = %f", xPosBut);
NSLog(#"scroll.frame.size.width = %f", scrollView.frame.size.width);
xPosition += scrollView.frame.size.width+2;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xPosition, 4, 2, scrollView.frame.size.height - 8)];
seperatorView.backgroundColor = [UIColor redColor];
[scrollView addSubview:seperatorView];
xPosition +=scrollView.frame.size.width+ 4;
[self.view addSubview:scrollView];
[scrollView addSubview:seperatorView];
[scrollView addSubview:testButton];
[builder addObject:testButton];
}
[scrollView setContentSize: CGSizeMake(xPosition, scrollView.frame.size.height)];
[self.view addSubview:scrollView];
set the for loop condition part as per your requirement.
hope this will help you.

Resources