IOS UIButton 代码创建

来源:互联网 发布:重庆时时开奖软件 编辑:程序博客网 时间:2024/05/20 06:22

// 创建按钮

UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //绘制形状


// 确定宽、高、XY坐标

CGRect frame;

frame.size.width = 100;

frame.size.height = 30;

frame.origin.x = 320 / 2 - 50;

frame.origin.y = 480 / 2 - 30;

[btn setFrame:frame];


// 设置Tag(整型)

btn.tag = 10;


// 设置标题

[btn setTitle:@"按钮" forState:UIControlStateNormal];


// 设置未按下和按下的图片切换

[btn setBackgroundImage:[UIImage imageNamed:@"bus.png"forState:UIControlStateNormal];

[btn setBackgroundImage:[UIImage imageNamed:@"plane.png"forState:UIControlStateHighlighted];


// 设置事件

[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];


// 设置背景色和透明度

[btn setBackgroundColor:[UIColor blackColor]];

[btn setAlpha:0.8];


// 或设置背景色和透明度

btn.backgroundColor = [[UIColor blackColorcolorWithAlphaComponent:0.8];

[self.view addSubview:btn];



//按钮响应事件

-(void)btnPressed:(id)sender

{

UIButton *myBtn = (UIButton *)sender;

if (myBtn.tag == 10)

{

// TODO:

}

}

0 0