zoukankan      html  css  js  c++  java
  • iOS开发之代码创建常用控件(UIButton、UILabel)的思路

    代码创建按钮UIButton

    (一)基本设置

            //创建中间“+”按钮

            UIButton *addBtn = [[UIButton alloc] init];

            //设置默认背景图片

            [addBtn setBackgroundImage:[UIImage imageNamed:@"AddButtonIcon"] forState:UIControlStateNormal];

            //设置按下时背景图片

            [addBtn setBackgroundImage:[UIImage imageNamed:@"AddButtonIcon-Active"] forState:UIControlStateHighlighted];

            //设置默认标题

        [addBtn setTitle:@"title" forState:UIControlStateNormal];

            //添加响应事件

            [addBtn addTarget:self action:@selector(addBtnDidClick) forControlEvents:UIControlEventTouchUpInside];

            //将按钮添加到TabBar

            [self addSubview:addBtn];

    (二)设置大小和位置

        //设置“+”按钮的位置

        self.addButton.centerX = self.centerX;

        self.addButton.centerY = self.height * 0.5 - 1.5 * AddButtonMargin;

        //设置“+”按钮的大小为图片的大小

        self.addButton.size = CGSizeMake(self.addButton.currentBackgroundImage.size.width, self.addButton.currentBackgroundImage.size.height);

    (三)实现点击事件处理方法addBtnDidClick

    代码创建UILabel:

        //创建并设置“+”按钮下方的文本为“添加”

        UILabel *addLbl = [[UILabel alloc] init];

        addLbl.text = @"添加";

        addLbl.font = [UIFont systemFontOfSize:10];

        addLbl.textColor = [UIColor grayColor];

        [addLbl sizeToFit];

        //设置“添加”label的位置

        addLbl.centerX = self.addButton.centerX;

        addLbl.centerY = CGRectGetMaxY(self.addButton.frame) + 0.5 * AddButtonMargin + 0.5;

        

        [self addSubview:addLbl];

    其他控件代码创建思路:设置大小、位置,设置基本状态(如背景图片,标题,文本大小,颜色),将该视图加入到父视图,最后实现相应的交互事件处理方法(如果需要的话)

  • 相关阅读:
    [转] 使用C#开发ActiveX控件
    [转] error LNK2026: 模块对于 SAFESEH 映像是不安全的
    Struts2详细说明
    a web-based music player(GO + html5)
    oracle_单向函数_数字化功能
    UVA 1364
    ORA-12545: Connect failed because target host or object does not exist
    左右v$datafile和v$tempfile中间file#
    二十9天 月出冲击黑鸟 —Spring的AOP_AspectJ @annotation
    Shell编程入门(再版)(在)
  • 原文地址:https://www.cnblogs.com/guitarandcode/p/5759148.html
Copyright © 2011-2022 走看看