UIButton边界和背景图像

时间:2009-12-20 04:33:42

标签: iphone cocoa-touch uibutton

我想要的:指示是否选择了UIButton的边框。

背景:我有一些UIButtons使用透明图像,而不是文本。这些是切换按钮(即可以打开或关闭)。

问题:除非您更改关于按钮的其他内容,否则UIButton类不会向用户显示是否选择了按钮。由于图像不随状态而变化,因此我需要每个图像中的两个,一个正常,一个选择,并为按钮的每个状态设置一个。这很烦人。我想我会改变背景图像,但这会删除按钮上漂亮的边框,我只是得到一个背景图像的矩形,你好。

我不喜欢可能的解决方案:

1)创建一个与UIButton边界匹配的背景,并将其用于选中。我不喜欢这个,因为它们不匹配,我很挑剔。

2)为每个按钮创建两个图像,基本相同但背景不同。这似乎是不必要的工作,并且由于这个问题反复出现,我想要一个未来的解决方案。

我希望有人现在已经找到了一个合适的解决方案。提前谢谢。

4 个答案:

答案 0 :(得分:5)

由于UIButton有两个图像层,一个图像和一个背景图像,我认为你可以通过仅使用两个背景图像来完成你想要的所有按钮。一个图像显示边框而另一个图像不显示。当控制状态改变时,将背景交换出来。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

如果你只想要边框,那么你只能选择使用两个图像作为两个状态,否则如果你的目的是区分两个状态,那么你可以通过改变所选按钮的一点点来做到这一点这将产生类似切换按钮的效果,您也可以禁用所选按钮,并在按下另一个按钮时再次启用它。

希望这会给你一个公平的想法。

答案 3 :(得分:0)

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