在專案當中如何對UIButton進行重新布局

2021-07-10 21:37:14 字數 2155 閱讀 6151

在重新布局之前,我們首先要弄清楚uibutton構成。

uibutton內部預設有個uiimageview、uilabel控制項,可以分別用下面屬性訪問:

@property(nonatomic,readonly,retain) uiimageview *imageview;

@property(nonatomic,readonly,retain) uilabel *titlelabel;

uibutton之所以能顯示文字,完全是因為它內部的titlelabel

uibutton的setimage:forstate:方法設定的顯示到了內部的imageview上

注意* 設定按鈕的文字或文字顏色,必須用下面的方法

- (void)settitle:(nsstring *)title forstate:(uicontrolstate)state;

- (void)settitlecolor:(uicolor *)color forstate:(uicontrolstate)state;

#warnning 不能直接拿到titlelabel設定文字和文字顏色,比如下面的做法是錯誤的:

button.titlelabel.text = @"12323";

button.titlelabel.textcolor = [uicolor redcolor];

* 設定按鈕內部的小,必須用下面的方法

- (void)setimage:(uiimage *)image forstate:(uicontrolstate)state;

button.imageview.image = [uiimage imagenamed:@"abc.png"];

假設我們要得到這樣的效果:

有兩種方案:

1、完全自定義(view+button+label)

2、對button 進行重新布局

在這裡主要講的是對button進行重新布局需要重寫layoutsubviews方法。而該方法在什麼時候進行觸發呢:

1、 init初始化不會觸發layoutsubviews

2、 addsubview會觸發layoutsubviews

3、 設定view的frame會觸發layoutsubviews,當然前提是frame的值設定前後發生了變化

4、 滾動乙個uiscrollview會觸發layoutsubviews

5、 旋轉screen會觸發父uiview上的layoutsubviews事件

6、 改變乙個uiview大小的時候也會觸發父uiview上的layoutsubviews事件

**:

#import "lfcustombutton.h"

const double lfbuttonscale =0.7;

@implementation lfcustombutton

-(void)layoutsubviews

viewcontroller.m

self.view.backgroundcolor = [uicolor darkgraycolor];

lfcustombutton *btn = [lfcustombutton buttonwithtype:uibuttontypecustom];

btn.frame = cgrectmake(100, 100, 100, 100);

//注意:在此不能使用[btn setbackgroundimage:[uiimage imagenamed:@"label_comment@3x"] forstate:0],否則達不到想要的效果

[btn setimage:[uiimage imagenamed:@"1111"] forstate:0];

[btn settitle:@"哈哈" forstate:0];

[btn settintcolor:[uicolor redcolor]];

[btn settitlecolor:[uicolor blackcolor] forstate:0];

[self.view addsubview:btn];

通過以上**,就達到了我們想要的目的了。

在REDIS當中LIST如何使用分析

redis可以往hash鍊錶中存資料,使用訊息佇列的時候用過,可以往這個裡插入資料,解決高併發的問題。list是乙個鍊錶結構,主要功能是 push pop 獲取乙個範圍的所有值等等 操作中的 key理解為鍊錶的名字,redis 的list 型別其實就是乙個每個子元素都是 string 型別的雙項鍊表...

詳解在vue專案當中繫結鍵盤事件

在vue專案當中,有時會遇到為文字輸入框或者button按鈕繫結鍵盤事件,最常見的就是enter回車事件。按照vue官網給出的方法是 v on keyup.enter 簡寫 keyup.enter 如果是繫結在元件上的話,需要加native修飾符 但是,這種方式只能是在獲取焦點的時候起作用,如果失去...

在vue專案中,對axios進行的封裝

由於專案需求,對axios進行了封裝。引入axios import axios from axios let cancel promisearr const canceltoken axios.canceltoken 請求 axios.interceptors.request.use config ...