Create a Button Programmatically and Set a Background Image

Create a button programmatically and set a background image

Your code should look like below

let image = UIImage(named: "name") as UIImage?
let button = UIButton(type: UIButtonType.Custom) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setImage(image, forState: .Normal)
button.addTarget(self, action: "btnTouched:", forControlEvents:.TouchUpInside)
self.view.addSubview(button)

Setting the background of an UIButton programmatically not working

In Xcode 13 ,UIButton has four type are Plain,Grain,Tinted,Filled .When you create a button in storyboard , button type automatically set with Plain that means new UIButton configurations is on (If you set one of them 4).So when you try to use old behaviours like

setBackgroundImage(..)
setImage(..)

This functions not gonna effect on this button. So If you want to use old behaviours you need to set style Plain to Default

How to set the button background image through code

for set background image for button which is in drawable folder then use below code

btn.setBackgroundResource(R.drawable.new_todo_image);

Set a button background image iPhone programmatically

Complete code:

+ (UIButton *)buttonWithTitle:(NSString *)title
target:(id)target
selector:(SEL)selector
frame:(CGRect)frame
image:(UIImage *)image
imagePressed:(UIImage *)imagePressed
darkTextColor:(BOOL)darkTextColor
{
UIButton *button = [[UIButton alloc] initWithFrame:frame];

button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];

UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = [UIColor clearColor];

return button;
}

UIImage *buttonBackground = UIImage imageNamed:@"whiteButton.png";
UIImage *buttonBackgroundPressed = UIImage imageNamed:@"blueButton.png";

CGRect frame = CGRectMake(0.0, 0.0, kStdButtonWidth, kStdButtonHeight);

UIButton *button = [FinishedStatsView buttonWithTitle:title
target:target
selector:action
frame:frame
image:buttonBackground
imagePressed:buttonBackgroundPressed
darkTextColor:YES];

[self addSubview:button];

To set an image:

UIImage *buttonImage = [UIImage imageNamed:@"Home.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:myButton];

To remove an image:

[button setBackgroundImage:nil forState:UIControlStateNormal];

Changing ImageButton's background programmatically

Use a selector for your ImageButton and set background from xml.

If you want to use Drawable Images:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true"
android:drawable="@drawable/pressed_bg" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/focused_bh" /> <!-- focused -->
<item android:drawable="@drawable/default_bg" /> <!-- default -->
</selector>

If you want to use just color:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/white" />
<item android:state_pressed="true" android:drawable="@android:color/white" />
<item android:drawable="@android:color/transparent" />
</selector>


Related Topics



Leave a reply



Submit