Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set background for UIButtons with dynamic width?
    primarykey
    data
    text
    <p>I have 3 button images named edit_category_left_up,edit_category_middle_up and edit_category_right_up <img src="https://i.stack.imgur.com/ll6Wd.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/GxrYm.png" alt="enter image description here"> <img src="https://i.stack.imgur.com/r9tej.png" alt="enter image description here"></p> <p>I have written code to create buttons with dynamic width now I need to set background of these button like this image below using the above 3 images</p> <p><img src="https://i.stack.imgur.com/IkTiT.png" alt="enter image description here"></p> <p>This is my current code to create dynamic buttons</p> <pre><code>-(void) addDynamicButtonsForCategories { _adcImage.hidden=YES; _makeADCLabel.hidden=YES; float x=0; float y=0; float frameHeight=_subADCView.frame.size.height; NSString *titleString=@""; for(int i = 0; i &lt; 15; i++) { UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; if (i&lt;1) { [btn setFrame:CGRectMake(0, 0, 39, 39)]; [btn setImage:[UIImage imageNamed:@"addcategory_up.png"] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:@"addcategory_down.png"] forState:UIControlStateHighlighted]; x = x + btn.frame.size.width+10; y=y+btn.frame.origin.y; [btn setTitle:titleString forState: UIControlStateNormal]; [btn setTag:i]; } else { titleString = [titleString stringByAppendingString:[NSString stringWithFormat:@"%d",i]]; // get button title CGSize fontSize = [titleString sizeWithFont:[UIFont systemFontOfSize:12.0]]; CGRect currentFrame = btn.frame; CGRect buttonFrame = CGRectMake(x, y, fontSize.width + 22.0, 40); if ((buttonFrame.origin.x+buttonFrame.size.width) &gt;_subADCView.frame.size.width) { x=0;y=_subADCView.frame.size.height; _subADCView.frame=CGRectMake(_subADCView.frame.origin.x, _subADCView.frame.origin.y, _subADCView.frame.size.width, _subADCView.frame.size.height+frameHeight); buttonFrame = CGRectMake(x, y, fontSize.width+ 22.0, 40); } x = x + fontSize.width + 35.0; y=y+currentFrame.origin.y; [btn setFrame:buttonFrame]; //I need to set the button image here [btn setImage:[[UIImage imageNamed:@"edit_category_middle_up.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateNormal]; [btn setImage:[[UIImage imageNamed:@"edit_category_middle_down.png"] stretchableImageWithLeftCapWidth:10 topCapHeight:0] forState:UIControlStateHighlighted]; [btn setTitle:titleString forState: UIControlStateNormal]; [btn setTag:i]; lastButtonPosition=btn.frame.origin.y+btn.frame.size.height; } [self.subADCView addSubview:btn]; } [self readjustSubviews]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload