UI第二节——UIButton详解

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   UIViewController *controller = [[UIViewController alloc]init];
    
    [self.window setRootViewController:controller];
    

    
    // 创建UIButton,唯独UIButton的创建不一样
    // UIButtonTypeSystem   系统默认的类型
    // UIButtonTypeCustom   用户自定义类型
    // UIButtonTypeContactAdd 文字前面会有一个 + 符号
    
    // UIButtonTypeDetailDisclosure 会有一个 i 符号
    // UIButtonTypeInfoLight
    // UIButtonTypeInfoDark
    
    // UIButtonTypeRoundedRect  跟UIButtonTypeSystem没有区别
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    
    // 设置它的frame(位置)
    btn.frame = CGRectMake(20, 30, 335, 30);
    
//    NSLog(@"%f, %f", btn.frame.size.width, btn.frame.size.height);
//    btn.frame.origin // 原点坐标
//    btn.frame.size   // 大小

    
    // 设置标题
    // UIControlStateNormal         普通状态
    // UIControlStateHighlighted    高亮状态
    [btn setTitle:@"This is a Button" forState:UIControlStateNormal];
    [btn setTitle:@"Heightlighted" forState:UIControlStateHighlighted];
    
    // 设置Button的字体
    btn.titleLabel.font = [UIFont systemFontOfSize:30];
    
    // 调整Title位置
    // top left bottom right 的偏移
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, -100, 0, 0);
    
    // 主题颜色
    btn.tintColor = [UIColor whiteColor];
    
    // 背景颜色
    btn.backgroundColor = [UIColor redColor];
    
    // 添加点击事件
    // 让 target(谁) 去做 action (什么事)
    // 让AppDelegate对象去执行 btnClicked: 这个函数
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    // 把Button添加到Window上
    [self.window addSubview:btn];
    
    
    /******
     第二个Button
     ******/
    
    UIButton *secondBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    secondBtn.frame = CGRectMake(20, 70, 335, 30);
    [secondBtn setTitle:@"Seconde Button" forState:UIControlStateNormal];
    
    // clearColor 透明颜色
    secondBtn.backgroundColor = [UIColor clearColor];
    
    // 设置图片
    
    // 加载项目中的图片
    UIImage *image = [UIImage imageNamed:@"bc_btn_blue"];
    
    NSLog(@"%f, %f", image.size.width, image.size.height);
    
    UIImage *heightlightedImage = [UIImage imageNamed:@"test"];
    
    // 设置Button的背景图片
    [secondBtn setBackgroundImage:image forState:UIControlStateNormal];
    [secondBtn setBackgroundImage:heightlightedImage forState:UIControlStateHighlighted];
    
    // 设置Button的图标
    // 图标这里,只会要图片的形状
    UIImage *iconImage = [UIImage imageNamed:@"IconEye"];
    UIImage *heightlightedIconImage = [UIImage imageNamed:@"IconFlag"];
    [secondBtn setImage:iconImage forState:UIControlStateNormal];
    [secondBtn setImage:heightlightedIconImage forState:UIControlStateHighlighted];

//    secondBtn.tintColor = [UIColor redColor];
    
    [self.window addSubview:secondBtn];
    
   self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)btnClicked:(UIButton *)btn
{
    NSLog(@"2222");

}

 如果对你有帮助,请关注我哦!

posted @ 2016-12-26 11:42  朝阳向日葵  阅读(141)  评论(0编辑  收藏  举报