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];

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

  • 相关阅读:
    IPC对象的持续性
    数据结构C++版-图
    数据结构C++版-树
    数据结构C++版-队列
    数据结构C++版-栈
    数据结构C++版-线性表
    转-二值图像连通域标记
    AdaBoost笔记之通俗易懂原理介绍
    AdaBoost笔记之原理
    C++之多态
  • 原文地址:https://www.cnblogs.com/guitarandcode/p/5759148.html
Copyright © 2011-2022 走看看