码迷,mamicode.com
首页 > 其他好文 > 详细

UIButton的几个常见属性

时间:2016-04-06 18:41:06      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

#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

 

UIButton的几个常见属性

标签:

原文地址:http://www.cnblogs.com/zhizunbao/p/5360096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!