zoukankan      html  css  js  c++  java
  • UIButton

     //父类为UIControl 是事件驱动型控件
    //buttonWithType 得到button对象的类方法
    //UIButtonTypeRoundedRect 圆角矩形的btn,在iOS7之前,iOS操作系统的界面风格为拟物化风格;iOS7之后界面风格为扁平化
    //iOS7设置圆角矩形效果,无效
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10,30,300,30);
    //设置标题
    //按钮会处于不同的状态: 默认为UIControlStateNormal;当按钮被点击的时候,处于UIControlStateHighlighted高亮状态;
    [btn setTitle:@"常规状态" forState:UIControlStateNormal];
    //设置btn处于高亮状态下的标题
    [btn setTitle:@"被点击" forState:UIControlStateHighlighted];
    //设置标题颜色
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    //设置字体
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:22]];
    //设置标记,UIView的属性
    btn.tag =100;
    //非常重要
    //forControlEvents 作用在btn上的事件
    //UIControlEventTouchUpInside 点击按钮,在btn的frame范围之内松开手指,对应的事件
    //事件驱动型控件: 当btn满足特定事件时,会让target对象执行action方法(id-SEL)
    //SEL中的方法 在id中必须存在,否则程序崩溃
    //给按钮添加一个点击事件
    //第一个参数名是目标对象(也就是给谁发消息),第二个参数是传一个方法名,(注意要这目标对象里有这个方法,不然就挂了)
    [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn];
    
    //UIButtonTypeCustom 自定义样式,使用频率最多,此时需要给btn贴图
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = [UIColor redColor];
    //获取图片对象,根据图片名称,加载图片,得到图片对象
    UIImage *img1 = [UIImage imageNamed:@"1.png"];
    //img1.size 获取图片的大小
    NSLog(@"image %f",img1.size.width);
    NSLog(@"image height:%f",img1.size.height);
    //将图片加到btn上,setImage 当btn的size大于图片的size时,图片不会被拉伸
    [btn1 setImage:img1 forState:UIControlStateNormal];
    
    UIImage *mapImg = [UIImage imageNamed:@"map.png"];
    //setBackgroundImage 图片的大小会与btn的size 保持一致
    [btn1 setBackgroundImage:mapImg forState:UIControlStateNormal];
    btn1.tag = 101;
    btn1.frame = CGRectMake(10,70,300,30);
    [btn1 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn1];
  • 相关阅读:
    集合——iterator迭代器
    集合——顶层collection接口(单列集合)
    集合——集合框架
    构造方法
    接口作为方法的参数或返回值——List接口
    接口作为成员变量——实现类和匿名内部类和匿名对象
    距离和相似度度量
    Kmeans算法与KNN算法的区别
    linux命令
    MapReduce初级案例
  • 原文地址:https://www.cnblogs.com/liudongyan/p/4399268.html
Copyright © 2011-2022 走看看