zoukankan      html  css  js  c++  java
  • UIButton

    按钮UIButton在app的出镜率也相当高。作用是响应用户点击的控件。

    创建UIButton,遵循一下几个步骤:

    1、一般用便利构造器来创建一个button对象。

    2、设置按钮的属性

    3、为按钮添加点击事件

    4、把按钮添加到父视图,得以显示

    5、无需释放。

    // 便利构造器创建一个button

        UIButton *button1 = [UIButton

    buttonWithType:UIButtonTypeSystem];

        button1.frame = CGRectMake(100, 100, 100, 50);

        button1.backgroundColor = [UIColor clearColor];

       

        // 设置button文字

        [button1 setTitle:@"点我撒" forState:UIControlStateNormal];

    //点击时才会显示

        [button1 setTitle:@"点我撒" forState:UIControlStateHighlighted];

    //隐形

        [button1 setTitle:@"点我撒" forState:UIControlStateSelected];

     

    // 设置图片

        // 必须使用镂空图片

        [button1 setImage:[UIImage imageNamed:@"user.png"] forState:UIControlStateNormal];

    //获取这个代替这个按钮的图片。

    UIImage *normalImage = [loginButton imageForState:UIControlStateNormal];

    //设置背景图片(不用镂空的)

        // 用来代替按钮

        [button1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:(UIControlStateNormal)];

    // 添加点击事件

        [button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

     [self.window addSubview:button1];

    // 点击button响应方法

    // 点击button的时候,会把button1当作参数传进来给sender

    - (void)buttonAction:(UIButton *)sender{

       //点击后产生随机颜色    self.window.backgroundColor = [UIColor colorWithRed: arc4random() % 256 / 255.0 green: arc4random() % 256 / 255.0 blue: arc4random() % 256 / 255.0 alpha:1];

     

        NSLog(@"GOOD");

        // 获取点击事件文字

        NSString *str = [sender titleForState:UIControlStateNormal];

       

        NSLog(@"%@",str);

        

    // 移除点击事件

        [sender removeTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        

        NSLog(@"%ld",sender.tag);

       

    }

    解释:

    1、self指的是指定本类来实现这个点击事件

    2、buttonAction方法来实现点击事件想要做的事。

    //设定指定状态下的button的标题,就是button里显示什么字。

    [loginButton setTitle:@“登录”forState:UIControlStateNormal];

    //获取指定状态下的button的标题,也就是button里的字。

    NSString *normalTitle = [loginButton titleForState:UIControlStateNormal];

    //设定某个状态下的button的标题颜色,就是button里的字的颜色。

    [loginButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    //获取某个状态下的button的标题的颜色,就是button里的字的颜色。////

    UIColor *normalTitleColor = [loginButton titleColorForState:UIControlStateNormal];

    //设定指定状态下的button的标题的阴影颜色

    [loginButton setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal];

    //获取指定状态下的标题阴影颜⾊////

    UIColor *normalTitleShadowColor = [loginButton titleColorForState:UIControlStateNormal];

    //用图片来代替按钮,这样代替的图片,必须是镂空的图片才有效果。

  • 相关阅读:
    zabbix4.2.5常见问题指南
    postgres常用运维sql
    centos7部署inotify与rsync实现实时数据同步
    postgres主从基于流复制
    postgres高可用学习篇三:haproxy+keepalived实现postgres负载均衡
    postgres高可用学习篇二:通过pgbouncer连接池工具来管理postgres连接
    postgres高可用学习篇一:如何通过patroni如何管理3个postgres节点
    centos7安装yum安装pip
    nginx1.15.10配置使用非https访问返回403
    zabbix4.2.5自定义告警模板
  • 原文地址:https://www.cnblogs.com/Coder-GT/p/4866258.html
Copyright © 2011-2022 走看看