zoukankan      html  css  js  c++  java
  • UIButton

    UIButton的相关操作:

         在实际的工作中,至少需要一个视图控制器(页面),不会直接往window上写UI,所有的UI都是写在视图控制器里的

    1.页面间的关联(写在AppDelegate中)

        HomeViewController *hvc = [[HomeViewController alloc] init];

        self.window.rootViewController = hvc;

    2.初始化button按钮

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];//UIButtonTypeSystem

         /*
         UIButtonTypeCustom = 0,             没有类型   无色无形态  一般用于自定义按钮
         UIButtonTypeDetailDisclosure         详细按钮
         UIButtonTypeInfoLight,              信息按钮
         UIButtonTypeInfoDark,               黑色信息按钮
         UIButtonTypeContactAdd,             加号按钮
         
         UIButtonTypeRoundedRect = UIButtonTypeSystem,   // Deprecated, use UIButtonTypeSystem instead
         
         */

        btn.frame = CGRectMake(,,,);

        btn.backgroundColor = [UIColor redColor];

    3.设置文字,默认状态

       [btn setTitle:@"dk" forState:UIControlStateNormal];

    4.设置文字颜色

       [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    5.设置背景图片

       [btn setBackgroundImage:[UIImage imageNamed:@"aaa"] forState:UIControlStateNormal];

    6.高亮状态(按住不放)下的文字、颜色、背景图

        [btn setTitle:@"高亮" forState:UIControlStateHighlighted];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [btn setBackgroundImage:[UIImage imageNamed:@"bbb"] forState:UIControlStateHighlighted];

    7.点击事件

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        //第一个参数是接收消息的对象,第二个参数是消息(方法),第三个参数是事件
        //OC的事件-消息机制
        //btn开始监听点击事件,当事件发生的时候,给某个对象发送消息

    8.设置字体

       btn.titleLabel.font = [UIFont systemFontOfSize:25];

    9.btn还可以设置tag值

        UIButton *btn = [self.view viewOfTag:sender.tag];

    10.btn还可以自定义,这个时候btn的类型一定要选择custom没有任何样式的

         UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];

         //添加图片
        UIImage *image = [UIImage imageNamed:@"tab_1.png"];//图片一定要写全称 连带后缀
        [btn1 setImage:image forState:UIControlStateNormal];//使用这个方法添加 如果按钮区域要比图片尺寸大 图片不会被拉升 原样显示   但是如 果图片比按钮大小要大 那就会被压缩
        
        UIImage *image1 = [UIImage imageNamed:@"tab_c1.png"];//图片一定要写全称 连带后缀
        [btn1 setImage:image1 forState:UIControlStateHighlighted];
        //setImage方法添加图片  title会和图片并排显示

  • 相关阅读:
    hdu 2222 Keywords Search
    Meet and Greet
    hdu 4673
    hdu 4768
    hdu 4747 Mex
    uva 1513 Movie collection
    uva 12299 RMQ with Shifts
    uva 11732 strcmp() Anyone?
    uva 1401
    hdu 1251 统计难题
  • 原文地址:https://www.cnblogs.com/Angelone/p/4384782.html
Copyright © 2011-2022 走看看