//创建圆角button
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
//指定button的位置和大小
button.frame = CGRectMake(10, 10, 75, 75);
//给button设置标签,用来辨别点击的是哪个button,常用在委托方法中。
button.tag = 0;
//定义按钮标题
[button setTitle:@"Button Title" forState:UIControlStateNormal];
//定义按钮标题字体格式
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
//给button添加委托方法,即点击触发的事件。
[button addTarget:selfaction:@selector(touchButton1:) forControlEvents :UIControl EventTouchUp Inside];
//给button添加图片
[button setImage:[UIImageimageNamed:@"blue_bg(2).png"] forState:UIControlStateNormal];
//将button加入视图当中
[view addSubview:button];
项目经常用到的.m文件可查找以下代码段:
//
// ViewController.m
// UIButtonDemo
//
// Created by Yen Joe on 12-2-29.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)btnPressed:(id)sender{
UIButton* btn = (UIButton*)sender;
//开始写你自己的动作
[[[[UIAlertView alloc]initWithTitle:@"Button" message:[NSString stringWithFormat: @"This is button:%@ action" ,btn]delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]autorelease]show];
}
- (void)viewDidLoad
{
[super viewDidLoad];
//两种不同的方式创建
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置Frame
CGRect btn2Frame = CGRectMake(100.0, 100.0, 60.0, 44.0);
btn2.frame =btn2Frame;
//设置Title
[btn1 setTitle:@"BTN1" forState:UIControlStateNormal];
[btn2 setTitle:@"BTN2" forState:UIControlStateNormal];
[btn2 setBackgroundImage:[UIImage imageNamed:@"pic.png"] forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//设置标题颜色
[btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//阴影
[btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景图像
btn1.adjustsImageWhenHighlighted = NO;
btn1.adjustsImageWhenDisabled = NO;
btn1.showsTouchWhenHighlighted = NO;
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];//添加动作
/* - (CGRect)imageRectForContentRect:(CGRect)bounds{
return CGRectMake(0.0, 0.0, 44, 44);
}*///重绘行为
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end