UIButton的简单使用

  //初始化

UIButton *myButton = [UIButton buttonWithType: UIButtonTypeCustom];

// button类型有以下6种,

// UIButtonTypeCustom = 0, 自定义风格

// UIButtonTypeRoundedRect, 圆角矩形

// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

// UIButtonTypeInfoLight, 亮色感叹号

// UIButtonTypeInfoDark, 暗色感叹号

// UIButtonTypeContactAdd, 十字加号按钮

 

//button设置frame

myButton.frame = CGRectMake(20, 20, 280,40);

 

//button背景色

myButton.backgroundColor = [UIColor clearColor];

 

//button背景图片

//[myButton setImage:[UIImage imageNamed:@"back.png" ] forState:UIControlStateNormal];

 

//设置button标题

[myButton setTitle:@"返回"  forState:UIControlStateNormal];

 

// forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

//以下是几种状态

// UIControlStateNormal = 0, 常规状态显现

// UIControlStateHighlighted = 1 <<0, 高亮状态显现

// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

// UIControlStateSelected = 1 << 2, 选中状态

// UIControlStateApplication = 0x00FF0000, 当应用程序标志时

// UIControlStateReserved = 0xFF000000 为内部框架预留


 

//当按钮高亮的情况下,图像的颜色较深,将下面的这个属性设置为no,高亮取消

myButton.adjustsImageWhenHighlighted = NO;


//当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置

myButton.adjustsImageWhenDisabled = NO;


//设置为yes,按钮按下会发光,设置为no,无效果

myButton.showsTouchWhenHighlighted = YES;

 

// 给button添加事件

[myButton addTarget:self action:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];

 

//显示

[self.view addSubview:myButton];

 

//button方法实现

-(void) btnClick:(UIButton*)btn{

NSLog(@”do something”);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值