Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIButton border and background image

What I Want: A border indicating if a UIButton is selected or not.

Background: I've got some UIButtons using transparent images, not text. These are toggle buttons (i.e. can be on or off).

Problem: The UIButton class gives users no indication of whether a button is selected or not unless you change something else about the button. Since the image doesn't change with the state, I'd need two of every image, one normal, one selected and set one for each state of the button. This is annoying. I thought instead I'd change the background image, but this removes the pretty border on the button, I just get a rectangle of my background image, yuck.

Possible solutions I don't like:

1) Create a background that matches the UIButton border and use that for selected. I don't like this because they wont match perfectly and I'm picky.

2) Create two images for each button, essentially identical but with a different background. This seems like unnecessary work, and since this problem is coming up repeatedly, I want a solution for the future as well.

I hope somebody's figured out a decent solution to this by now. Thanks in advance.

like image 245
David Kanarek Avatar asked Dec 20 '09 04:12

David Kanarek


2 Answers

Since UIButton has two image layers, an image and a background image, I think you could accomplish what you want by using just two background images for all your buttons. One image shows a border and the other does not. Swap the backgrounds out when the control state changed.

like image 178
TechZen Avatar answered Sep 29 '22 03:09

TechZen


//
//  TabBarSingleton.h

#import <Foundation/Foundation.h>

@interface TabBarSingleton : UITabBarController <UITabBarControllerDelegate>{
    NSRecursiveLock *barLock;

    UIButton *Button;
    UIButton *favoriteButton;
}

@property(nonatomic, retain) UIButton *Button;
@property(nonatomic, retain) UIButton *favoriteButton;

- (void) ButtonPressed;
- (void) favoriteButtonPressed;

@end

///////////////////////////////////
like image 23
Arun Dhwaj Avatar answered Sep 29 '22 05:09

Arun Dhwaj