UIButton的几个常见属性

本文详细介绍了在iOS开发中如何使用UIButton进行自定义样式设置,包括背景图片、边框圆角、边框宽度及字体颜色等,并实现了按钮点击事件的响应与处理。通过实例代码展示了UIButton的创建、属性配置、事件添加及事件处理过程。
#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    self.window.rootViewController = [[ViewController alloc] init];
    
    
#pragma mark - UIButton
    // 1.创建对象并初始化
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    
    // 2.设置属性
    button.frame = CGRectMake(50, 100, 250, 250);
    //button.backgroundColor = [UIColor orangeColor];
    button.layer.cornerRadius = 10;   // 边框圆角
    button.layer.borderWidth = 2;   // 边框边线宽度
    //button.tintColor = [UIColor blackColor];  // 设置字体颜色
    [button setTitle:@"点我啊" forState:UIControlStateNormal];   // 设置字体
    [button setTitle:@"点你咋地" forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];  // 设置字体颜色
    UIImage *image1 = [UIImage imageNamed:@"biaoqingdi副本.png"];
    [button setBackgroundImage:image1 forState:UIControlStateNormal];  // 设置背景图片
    [button setBackgroundImage:[UIImage imageNamed:@"chiniupa副本"] forState:UIControlStateHighlighted];
    [button setImage:[UIImage imageNamed:@"222"] forState:UIControlStateNormal];  // 设置前景图片(必须是镂空图片)
    
    
    // 3.添加事件
    // 单击状态下
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
    // 4.添加到父视图
    [self.window addSubview:button];
    
    return YES;
}


// 实现按钮点击事件
- (void)click:(UIButton *)sender {
    NSLog(@"哈哈哈");
    sender.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    
    // 移除事件
    [sender removeTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}

@end

 

转载于:https://www.cnblogs.com/zhizunbao/p/5360096.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值