Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove background image to a button

Tags:

iphone

ipad

I added 10 buttons to a view (example view name is "menuView"), now I want to remove the background image for 2nd, 3rd, 4th buttons. I wrote the code like this

for(id btn in [menuView subViews]){

       [btn setBackgroundImage:nil forState:UIControlStateNormal];

 }

The problem with this code is, it is removing all 10 button's backGroundimage, but I need to set nil for the 2nd, 3rd, and 4th buttons

like image 585
Prasad Avatar asked Jun 19 '11 13:06

Prasad


People also ask

How do I remove the background of an image?

Select the picture that you want to remove the background from. On the toolbar, select Picture Format > Remove Background, or Format > Remove Background. If you don't see Remove Background, make sure you have selected a picture.

How do I remove the background from a logo?

Click the button "Make Logo Transparent Now". Find the "Background Remover" in the tools menu on the left. Then, upload or drag and drop your logo from your computer. Fotor will remove background from logo automatically.

How do you remove the background of an inserted image in PowerPoint?

Go ahead and: Select your picture by clicking on it. Now go to > Picture Format in the top bar of PowerPoint. Select Remove Background.


2 Answers

If you create a tag for the buttons that you add, you can filter against them.

for(UIButton *btn in [menuView subViews]){
    if (btn.tag == 2 || btn.tag == 3 || btn.tag == 4) {
       [btn setBackgroundImage:nil forState:UIControlStateNormal];
    }
 }

Of course, you need to make sure that there are no other views in menuView that could share the same tag. So the choices are to make the tags large, unique values, or checking that they are actually UIButtons. I've edited this assuming that the only subviews of menuView are UIButtons. Enumerating over UIButtons won't cause compiler warnings about tag not being a property of NSObject.

UIButton is a subclass of UIControl which is a subclass of UIView. UIView has the tag property, so UIButton inherits this property. It's useful to look at the docs for a class that you are using, and continue up the hierarchy to see if there are properties or methods that are useful for what you need to do.

like image 98
Abizern Avatar answered Sep 28 '22 02:09

Abizern


Just to expand on my comment.

Using an IBOutletCollection which you can point an array to many objects in the nib. You declare this as such (synthesizing in the implementation):

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *threeButtons;

This says to IB that it's a collection of UIButton elements. In IB, you connect this to the three buttons you wish to remove the background image for, by control dragging from it to the buttons. Once this is done the array will contain those buttons you connected up and you can loop like so:

for (UIButton *button in self.threeButtons) {
    [button setBackgroundImage:nil forState:UIControlStateNormal];
}

Again, the link to a more detailed explanation can be found at: http://bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection

like image 27
Daniel Tull Avatar answered Sep 28 '22 01:09

Daniel Tull