开发者

UIButton border and background image

开发者 https://www.devze.com 2022-12-14 16:16 出处:网络
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).

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 no开发者_运维百科w. Thanks in advance.


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.


//
//  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

///////////////////////////////////


If you want the the borders only, then you have only one choice of using two images for the two states otherwise if your purpose is to differentiate between two states then you can do it by changing alpha a little bit of the selected button this will give the effect like toggle buttons, you can also disable the selected button and enable it again when the other button is pressed.

Hope this will give you a fair idea.


//
//  TabBarSingleton.m

//  Created by ArunDhwaj on 9/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//


#import "TabBarSingleton.h"

@implementation TabBarSingleton

@synthesize Button, favoriteButton;


- (id) init
{
    if (self = [super initWithNibName: nil bundle: nil]) 
    {
        barLock = [[NSRecursiveLock alloc] init];
    }
    self.delegate = self;
    return self;
}

+ (TabBarSingleton *) defaultBar
{
}

- (void)viewDidLoad
{
    NSLog(@"TabBarSingleton: viewDidLoad");

    //Hiding TabBar 
    self.tabBar.hidden = YES;

    //Creating a UIView, its frame is same as tabBar frme
    CGRect tabbarFrame = self.tabBar.frame; 
    UIView* customTabbarView = [[UIView alloc] initWithFrame:tabbarFrame];

    UIImageView *newsFeedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newsfeeds_normal.png"]];
    newsFeedImg.frame = CGRectOffset(newsFeedImg.frame, 0, 1);

    Button = [UIButton buttonWithType:UIButtonTypeCustom];
    [Button setFrame:newsFeedImg.frame];

    [Button setBackgroundImage:newsFeedImg.image forState:UIControlStateNormal];
    [Button setBackgroundImage:[UIImage imageNamed:@"newsfeeds_active.png"] forState:UIControlStateHighlighted];     
    [Button addTarget:self action:@selector(newsFeedsButtonPressed) forControlEvents:UIControlEventTouchUpInside];

    [customTabbarView addSubview:Button];
    //[newsFeedImg release];

    CGRect newsFeedFrame = newsFeedImg.frame;
    UIImageView *favoriteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites_normal.png"]];
    favoriteImg.frame = CGRectMake(newsFeedFrame.size.width, newsFeedFrame.origin.y, newsFeedFrame.size.width, newsFeedFrame.size.height);

    favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [favoriteButton setFrame:favoriteImg.frame];

    [favoriteButton setBackgroundImage:favoriteImg.image forState:UIControlStateNormal];
    [favoriteButton setBackgroundImage:[UIImage imageNamed:@"favorites_active.png"] forState:UIControlStateHighlighted];     
    [favoriteButton addTarget:self action:@selector(favoriteButtonPressed) forControlEvents:UIControlEventTouchUpInside];

    [customTabbarView addSubview: favoriteButton];
    //[favoriteImg release];

    [self.view addSubview:customTabbarView ];

    [self newsFeedsButtonPressed];
}


- (void) newsFeedsButtonPressed
{
    NSLog(@"TabBarSingleton: newsFeedsButtonPressed");
    self.selectedIndex = 0;

    //Keeping Highlighted newsFeed tab
    UIImageView *newsFeedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newsfeeds_active.png"]];
    [Button setImage: newsFeedImg.image forState:UIControlStateNormal];

    //Keeping normal others tab icons 
    UIImageView *favoriteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites_normal.png"]];
    [favoriteButton setImage: favoriteImg.image forState:UIControlStateNormal];
}

- (void) favoriteButtonPressed
{
    NSLog(@"TabBarSingleton: favoriteButtonPressed");
    self.selectedIndex = 1;

    //Keeping Highlighted newsFeed tab
    UIImageView *newsFeedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newsfeeds_normal.png"]];
    [Button setImage: newsFeedImg.image forState:UIControlStateNormal];

    //Keeping normal others tab icons 
    UIImageView *favoriteImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"favorites_active.png"]];
    [favoriteButton setImage: favoriteImg.image forState:UIControlStateNormal];

#pragma mark  UITabBarControllerDelegate 

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"TabBarSingleton: shouldSelectViewController");
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSLog(@"TabBarSingleton: didSelectViewController");
}

- (void) dealloc
{
    //[barLock release];
    [super dealloc];
}

@end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号