ui怎么设置button被选中后的背景颜色

 我来答
就烦条0o
2016-01-19 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46487
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部
1,通过按钮的事件来设置背景色

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(50, 200, 100, 50)];
[button1 setTitle:@"button1" forState:UIControlStateNormal];
button1.backgroundColor = [UIColor orangeColor];
[button1 addTarget:self action:@selector(button1BackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
[button1 addTarget:self action:@selector(button1BackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}

// button1普通状态下的背景色
- (void)button1BackGroundNormal:(UIButton *)sender
{
sender.backgroundColor = [UIColor orangeColor];
}

// button1高亮状态下的背景色
- (void)button1BackGroundHighlighted:(UIButton *)sender
{
sender.backgroundColor = [UIColor greenColor];
}

2,通过把颜色转换为UIImage来作为按钮不同状态下的背景图片

- (void)viewDidLoad {
[super viewDidLoad];

UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(170, 200, 100, 50)];
[button2 setTitle:@"button2" forState:UIControlStateNormal];
[button2 setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
[button2 setBackgroundImage:[self imageWithColor:[UIColor grayColor]] forState:UIControlStateHighlighted];
[self.view addSubview:button2];
}

// 颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image;
}
匿名用户
2016-01-19
展开全部
//
// ViewController.m
// 通过代码导入图片
//
// Created by yons on 14-5-24.
// Copyright (c) 2014年 wangjunzhuang. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建按钮
UIButton *btn=[[UIButton alloc]init];
NSLog(@"viewdidiload-----%p",btn);
//设置按钮的尺寸、位置
btn.frame=CGRectMake(0, 0, 100, 100);
//设置按钮的背景图片 普通状态下的
UIImage *normal=[UIImage imageNamed:@"t15.tiff"];
[btn setBackgroundImage:normal forState:UIControlStateNormal];
//设置文字和颜色
[btn setTitle:@"点我吧!" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//创建高亮状态下的按钮
//设置按钮的背景图片
UIImage *hight=[UIImage imageNamed:@"bd11 21-45-10-244.tiff"];
[btn setBackgroundImage:hight forState:UIControlStateHighlighted];
//设置文字和颜色
[btn setTitle:@"不要啊!" forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//监听按钮点击
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
//传到父类
[self.view addSubview:btn];
//创建一个文本框
UITextField *mytext=[[UITextField alloc]init];
//设置文本框的大小
mytext.frame=CGRectMake(100, 100, 100, 50);
//给文本框背景颜色
mytext.backgroundColor=[UIColor blueColor];
//给文本框设置在中点的位置
CGFloat centerX=self.view.frame.size.width*0.5;
CGFloat centerY=self.view.frame.size.height*0.5;
mytext.center=CGPointMake(centerX, centerY);
mytext.tag=99;
//设置字体大小
mytext.font=[UIFont systemFontOfSize:30];
//将对象添加到父类
[self.view addSubview:mytext];

}
#pragma mark 监听按钮点击的状态
-(void)btnClick:(UIButton *)btn
{
NSLog(@"-----%p",btn);//打印被点击地址。
}
#pragma mark 点击屏幕任意位置收起键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式