UIButton

UIButton

import “AppDelegate.h”

@interface AppDelegate ()

@property(nonatomic,assign)BOOL isSelected;
@property(nonatomic,assign)BOOL isClick;

@end

@implementation AppDelegate

-(void)dealloc{
[_window release];
[super dealloc];
}

  • (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions {
    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColorcyanColor];
    [self.windowmakeKeyAndVisible];
    [_window release];

    // 用button自己的便利构造器的方式来创建对象
    UIButton *button=[UIButtonbuttonWithType:UIButtonTypeSystem];
    // 指定button的位置和大小
    button.frame=CGRectMake(138,100, 100, 80);
    // 设置背景颜色
    button.backgroundColor=[UIColorredColor];
    [self.windowaddSubview:button];
    // button不用release

    // 给button设置标题
    [button setTitle:@”张三SB吗?”forState:UIControlStateNormal];
    // 给字体添加颜色
    [button setTitleColor:[UIColoryellowColor] forState:UIControlStateNormal];

    // 标题的字体大小
    button.titleLabel.font=[UIFontsystemFontOfSize:20];

    // 设置圆角
    button.layer.cornerRadius=20;

    // 边框
    button.layer.borderWidth=1;

    // 点击方法 butten最重要的方法
    [button addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];

// 重新创建一个button
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(50,50, 150, 90);
button.backgroundColor=[UIColorredColor];
[self.windowaddSubview:button];

[button setTitle:@"确认"forState:UIControlStateNormal];
[button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
button.layer.borderWidth=1;
button.layer.cornerRadius=10;
button.titleLabel.font=[UIFontsystemFontOfSize:20];
[button addTarget:selfaction:@selector(changePic:)forControlEvents:UIControlEventTouchUpInside];
// 设置一个tag值
button.tag=1000;

// 给button设置背景图片
[button setBackgroundImage:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];
self.isSelected=YES;







UIButton *button1=[UIButtonbuttonWithType:UIButtonTypeCustom];
button1.frame=CGRectMake(100,300, 50, 40);
button1.backgroundColor=[UIColoryellowColor];
[self.windowaddSubview:button1];
[button1 setTitle:@"测试"forState:UIControlStateNormal];
[button1 setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
button1.layer.borderWidth=1;
button1.layer.cornerRadius=10;
button1.titleLabel.font=[UIFontsystemFontOfSize:20];
[button1 addTarget:selfaction:@selector(changeImage:)forControlEvents:UIControlEventTouchUpInside];
button1.tag=1001;

// 给button设置背景图片
[button setBackgroundImage:[UIImageimageNamed:@"checked.png"]forState:UIControlStateNormal];

[button1 setImage:[UIImageimageNamed:@"BtnOff.png"]forState:UIControlStateNormal];
// 如果想使用setImage设置图片的话,button的类型要调整成,custom,setImage方法不会把图片放大成按钮大小
self.isClick=NO;




return YES;

}

-(void)click:(UIButton *)button{
NSLog(@”恩,他是SB!!!”);
}
// 更换按钮的背景图片
-(void)changePic:(UIButton *)button{
// 判断
// 根据不同的状态更换图片
if (self.isSelected) {
[button setBackgroundImage:[UIImageimageNamed:@”check.png”]forState: UIControlStateNormal];
}else{
[button setBackgroundImage:[UIImageimageNamed:@”checked.png”]forState: UIControlStateNormal];
}
// 把当前的状态也进行调整
// 取反
self.isSelected=!self.isSelected;
}

-(void)changeImage:(UIButton *)button{
if (self.isClick) {
[button setImage:[UIImageimageNamed:@”BtnOff.png”]forState:UIControlStateNormal];
}else{
[button setImage:[UIImageimageNamed:@”BtnOn.png”]forState:UIControlStateNormal];
}
self.isClick=!self.isClick;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值