UI-基礎(chǔ)控件

學(xué)習(xí)UI兩天后基本掌握了一些UI的基本控件用法。先說(shuō)明一下,我學(xué)的是iOS 不是IOS,不是ios,也不是IoS等等款七八糟的寫法。

下面既是我對(duì)簡(jiǎn)述也是自己對(duì)這兩天學(xué)習(xí)的回顧:(以下所有代碼全是在xcode上可運(yùn)行的,我用的是xcode7.2)

先說(shuō)UIWindow:

1.UIWindow: 在一個(gè)程序中有且只有一個(gè)主window

?1>取消程序的可視化入口,Main.sb

?2>創(chuàng)建主window對(duì)象

我們創(chuàng)建主 window,使其大小和屏幕一樣

UIScreen 系統(tǒng)屏幕類

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

給你創(chuàng)建的窗口填顏色 UIcolor 系統(tǒng)的顏色類

_window.backgroundColor =[UIColor blueColor];

//讓window 顯示,并成為主窗口 并 顯示

[_window makeKeyAndVisible];

4.設(shè)置window主窗口

在xcode7 之后 必須設(shè)置rootViewController,否則,在該方法結(jié)束的時(shí)候,會(huì)崩潰

_window.rootViewController = [[UIViewController alloc]init];

?3>UIwindow 繼承于UIView

綜上所述,開始一個(gè)需要UI設(shè)計(jì)布局的程序時(shí),啥都不用想,只要建好工程并在AppDelegate.m的文件里找到:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

return YES;

}

然后再 { 之后開始新建我們的主窗口,即主Window對(duì)象:

(1)UIScreen 系統(tǒng)屏幕類

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

2)給你創(chuàng)建的窗口填顏色 UIcolor 系統(tǒng)的顏色類

_window.backgroundColor =[UIColor blueColor];

(3)讓window 顯示,并成為主窗口并顯示

[_window makeKeyAndVisible];

(4).設(shè)置window主窗口

注意:(在xcode7 之后 必須設(shè)置rootViewController,否則,在該方法結(jié)束的時(shí)候,會(huì)崩潰)

_window.rootViewController = [[UIViewController alloc]init];


?2.UIView (

?UIView 與NSObject相似,是UI階段的根類,大部分控件繼承與UIView

首先我們先要添加一個(gè)UIView,方法如下:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

接著去給這個(gè)UIView添加背景色

view.backgroundColor = [UIColor greenColor];

讓我們直接將UIView加到_Window上

[_window addSubview:view];

遇到UIView的子類,都看完一使用以上方式創(chuàng)建和顯示

?這個(gè)UIView在屏幕中顯示為矩形,因?yàn)樵趧?chuàng)建的識(shí)貨我在其中定義了四個(gè)數(shù)值(0,0,100,100)分別是(x,y,width,heigh)

在這里值得一說(shuō)的是這個(gè)坐標(biāo)系。我們?cè)跀?shù)學(xué)的學(xué)習(xí)中也接觸過(guò)坐標(biāo)系,但千萬(wàn)不要以為是一樣的 完全是兩個(gè)概念得坐標(biāo)系。

iOS系統(tǒng)坐標(biāo)系

1.與數(shù)學(xué)中的坐標(biāo)系不同 ,y軸的正方向是向下的

2.原點(diǎn)是屏幕的左上角

接著說(shuō)我們的UIView

UIView的基本屬性

UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

[_window addSubview:testView];

背景色

testView.backgroundColor = [UIColor cyanColor];

alpha 透明度0。0~1.0 默認(rèn)值 1.0

testView.alpha = 0.2;

3.tag 標(biāo)記(編號(hào))(默認(rèn)為0)

testView.tag = 1000;

根據(jù)tag獲取到對(duì)應(yīng)的view

UIView *view1 = [_window viewWithTag:1000];

知道怎么設(shè)計(jì)view了 我們就說(shuō)說(shuō)視圖層級(jí)的關(guān)系

先做出三個(gè)視圖

UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

firstView.backgroundColor = [UIColor orangeColor];

UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];

secondView.backgroundColor = [UIColor redColor];

UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];

lastView.backgroundColor = [UIColor blackColor];

addSubview(添加視圖)

[_window addSubview:firstView];

insterSubview:atIndex(插入視圖,到指定下標(biāo))

NSLog(@"%@",_window.subviews);

[_window insertSubview:secondView atIndex:2];

insterSubview:aboveSubview(插入視圖,到指定視圖的上面)

[_window insertSubview:lastView aboveSubview:firstView];

insterSubview:belowSubview(插入視圖,到指定視圖的下面)

[_window insertSubview:lastView belowSubview:secondView];


視圖說(shuō)的差不多了 ,下面回憶一下UILable

什么是 UILable 說(shuō)白了就是負(fù)責(zé)顯示文本的

如果沒有自己的初始化方法,則使用父類的

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 100)];

lable.backgroundColor = [UIColor yellowColor];

[_window addSubview:lable];

讓我們看看 lable的屬性

?1.text

lable.text = @"我叫王煌,我很熱;看能否兼顧和歐文哦啊;的看來(lái)是你發(fā)幾個(gè)戶外;日歷卡價(jià)格和銳拼接開關(guān)后唯品;人均國(guó)內(nèi)宏觀;";

?2.textColor 文本顏色

lable.textColor = [UIColor lightGrayColor];

3.font 默認(rèn)值 17

lable.font = [UIFont systemFontOfSize:40];

lable.font = [UIFont boldSystemFontOfSize:40];

.textAlignment 對(duì)齊方式 默認(rèn)對(duì)齊方式NSTextAlignmentLeft

lable.textAlignment = NSTextAlignmentRight;

5.numberOfLines

不確定行數(shù)時(shí),給0

lable.numberOfLines = 0;

6.換行模式,。。。位置 lineBreakMode

lable.lineBreakMode = NSLineBreakByCharWrapping;

7.陰影顏色

lable.shadowColor = [UIColor redColor];

lable.shadowOffset = CGSizeMake(2, 4);

靜態(tài)的一些控件差不多說(shuō)到這,下面說(shuō)說(shuō)那個(gè)按鈕,就是button

什么叫UIButton呢?

直接點(diǎn)就是用來(lái)點(diǎn)擊的

//UIButtonTypeSystem系統(tǒng)按鈕類型

定義一個(gè)button

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

給按鈕設(shè)計(jì)顏色

button.backgroundColor = [UIColor purpleColor];

[_window addSubview:button];

定義button的位置

button.hl_x = 150;

button.hl_y = 300;

button.hl_height = 100;

button.hl_width = 100;

[_window addSubview:button];

//給Button添加事件

[button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];

//給button添加文字

[button setTitle:@"你點(diǎn)我啊~" forState:UIControlStateNormal];

[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

還要在}之外給button定義好buttonClick的方法


次外還有個(gè)可以直接顯示文字的控件就是UITextField

UITextField

UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(150, 450, 100, 100)];

textField.backgroundColor = [UIColor yellowColor];

[_window addSubview:textField];

1.給輸入框附了初值

textField.text = @"文字";

2.text.Color 文字顏色

textField.textColor = [UIColor blackColor];

?3.borderStyle 邊緣樣式

//UITextBorderStyleNone 無(wú)

//UITextBorderStyleLine 有邊緣線

//UITextBorderStyleBezel

//UITextBorderStyleRoundedRect 邊緣圓角

textField.borderStyle = UITextBorderStyleLine;

4.placeholder 占位字符

//當(dāng)text、不為空時(shí),placeholder 是不顯示的

textField.placeholder = @"請(qǐng)輸入你的愛好";

5.clearsOnBeginEdtting

textField.clearsOnBeginEditing = YES;

//command + K 顯示、收起鍵盤

textField.delegate = self;

但在}之外要聲明這個(gè)textfield的功能

//開始編輯前

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

//return YES 可以編輯

//return NO 不可以編輯

return YES;

}

//已經(jīng)開始編輯

- (void)textFieldDidBeginEditing:(UITextField *)textField {

}

- (void)textFieldDidEndEditing:(UITextField *)textField {

}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

//處理點(diǎn)擊return的時(shí)候,需要進(jìn)行的操作

//回收鍵盤

return YES;

}

以上就是我目前掌握的一些基礎(chǔ)控件,UIWindow,UIView,UIButton, UITextField,UILable

在知道以上控件后,我們只要記住怎么實(shí)用就好,首先要聲明一下,定義初始化,然后設(shè)置顏色,位置,大小,或者一些細(xì)節(jié)上的問題,最后在表明一下 這個(gè)控件是現(xiàn)實(shí)在哪個(gè)視圖上面的就好了!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容