iOS開發之代碼建立常用控制項(UIButton、UILabel)的思路

來源:互聯網
上載者:User

標籤:

代碼建立按鈕UIButton:

(一)基本設定

        //建立中間“+”按鈕

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

        //設定預設背景圖片

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

        //設定按下時背景圖片

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

        //添加響應事件

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

 

其他控制項代碼建立思路:設定大小、位置,設定基本狀態(如背景圖片,標題,文字大小,顏色),將該視圖加入到父視圖,最後實現相應的互動事件處理方法(如果需要的話)

iOS開發之代碼建立常用控制項(UIButton、UILabel)的思路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.