Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UIButton边框和背景图像_Iphone_Cocoa Touch_Uibutton - Fatal编程技术网

Iphone UIButton边框和背景图像

Iphone UIButton边框和背景图像,iphone,cocoa-touch,uibutton,Iphone,Cocoa Touch,Uibutton,我想要的是:一个边框,指示是否选择了UIButton 背景:我有一些使用透明图像而不是文本的UIButton。这些是切换按钮(即可以打开或关闭) 问题:UIButton类不会向用户提供按钮是否被选中的指示,除非您更改按钮的其他内容。由于图像不随状态变化,我需要每个图像中的两个,一个正常,一个选中,并为按钮的每个状态设置一个。这很烦人。我原以为我会改变背景图像,但这会删除按钮上漂亮的边框,我只得到一个矩形的背景图像,恶心 可能的解决方案我不喜欢: 1) 创建一个与UIButton边框匹配的背景,并

我想要的是:一个边框,指示是否选择了UIButton

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

问题:UIButton类不会向用户提供按钮是否被选中的指示,除非您更改按钮的其他内容。由于图像不随状态变化,我需要每个图像中的两个,一个正常,一个选中,并为按钮的每个状态设置一个。这很烦人。我原以为我会改变背景图像,但这会删除按钮上漂亮的边框,我只得到一个矩形的背景图像,恶心

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

1) 创建一个与UIButton边框匹配的背景,并将其用于选定对象。我不喜欢这样,因为它们不会完美匹配,而且我很挑剔

2) 为每个按钮创建两个基本相同但背景不同的图像。这似乎是不必要的工作,而且由于这个问题一再出现,我也希望将来有一个解决方案


我希望现在已经有人想出了一个像样的解决办法。提前感谢。

由于UIButton有两个图像层,一个图像层和一个背景图像层,我认为您可以通过为所有按钮使用两个背景图像来完成所需操作。一个图像显示边框,另一个不显示边框。当控件状态更改时,将背景调出。

如果您只需要边框,那么您只有一个选择,可以使用两个图像来表示两个状态,否则如果您的目的是区分两个状态,那么您可以通过更改所选按钮的一点alpha来实现,这将产生类似切换按钮的效果,您还可以禁用所选按钮,并在按下另一个按钮时再次启用该按钮

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

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

///////////////////////////////////
//TabBarSingleton.h #进口 @接口选项卡BARSingleton:UITabBarController{ NSRecursiveLock*条锁; UIButton*按钮; UIButton*收藏夹按钮; } @属性(非原子,保留)UIButton*按钮; @属性(非原子,保留)UIButton*favoriteButton; -(无效)按下按钮; -(void)已按下FavoriteButton; @结束 ///////////////////////////////////
这似乎是最好的选择。我创建了两个背景,它们非常匹配,并且非常符合常规的UIButton形状。您可能应该在代码中添加一些描述或注释来描述如何解决问题。这通常是这里的首选。当然,一个功能齐全的代码片段是一个很好的补充,但它本身并不能真正帮助提出问题的人理解解决方案。
//
//  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