UI基礎(chǔ)控件篇

創(chuàng)建window?

創(chuàng)建window 和當(dāng)前屏幕一樣大[UIScreen mainScreen].bounds

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

?設(shè)置window顏色

self.window.backgroundColor = [UIColor whiteColor];

?設(shè)置window可見

[self.window makeKeyAndVisible];

?xcode7 崩潰解決

self.window.rootViewController = [[UIViewController alloc] init];

?內(nèi)存管理

[_window release];

?UIView(視圖基類)

1.創(chuàng)建視圖 設(shè)置frame

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

?2.設(shè)置屬性

firstView.backgroundColor = [UIColor redColor];

?3.添加到父視圖(window)顯示

[self.window addSubview:firstView];

?4.內(nèi)存管理

[firstView release];

view 屬性

顯示/隱藏 hidden

aView.hidden = NO;

?透明度 alpha(0-1的浮點數(shù))

aView.alpha = 1;

?動畫

?[UIView animateWithDuration:1 animations:^{

? ? ?aView.alpha = 0;

? ? ?aView.frame = CGRectMake(0, 0, 200, 200);

? ? }];

?標(biāo)記值 tag

aView.tag = 1000;

?通過標(biāo)記尋找視圖

UIView *tempView = [self.window viewWithTag:1000];

tempView.backgroundColor = [UIColor purpleColor];

定時器NSTimer

每隔一段時間 讓某某做某事

參數(shù)1: 時間間隔

參數(shù)2: 某某

參數(shù)3: 某事

?[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(suibian) userInfo:nil repeats:YES];


UILabel

?1.創(chuàng)建+frame

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];

?2.屬性設(shè)置

label.backgroundColor = [UIColor yellowColor];

?3.添加父視圖

[self.window addSubview:label];

4.內(nèi)存管理

[label release];

label 文字相關(guān)屬性

顯示文字 ?( 默認 左對齊/居中顯示/文本黑色/行數(shù)為1/背景透明色)

label.text = @"這是一個label";

文本顏色 textColor

label.textColor = [UIColor purpleColor];

文本對齊方式?

label.textAlignment = NSTextAlignmentCenter;

斷行模式(文本省略方式) lineBreakMode

label.lineBreakMode = NSLineBreakByTruncatingMiddle;

文本行數(shù) numberOfLines ( 默認為1 為0時行數(shù)不限)

label.numberOfLines = 0;

?字體 font

label.font = [UIFont systemFontOfSize:30];

陰影 shadow

label.shadowColor = [UIColor blueColor];

?陰影偏移量

label.shadowOffset = CGSizeMake(10, 10);


UIButton

1.創(chuàng)建(便利構(gòu)造器方法)

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

?2.設(shè)置frame

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

3.設(shè)置屬性

btn.backgroundColor = [UIColor redColor];

?4.綁定按鈕點擊事件(按鈕被點擊時 能夠觸發(fā)一個方法)

?參數(shù)1: target 目標(biāo)(調(diào)用方法的人)

?參數(shù)2: action 動作(調(diào)用的方法)

?參數(shù)3: events 事件(方法的觸發(fā)條件)

UIControlEventTouchUpInside 控制事件之 觸摸頂端按下去

“ :” 參數(shù)標(biāo)志(之后一定會跟隨一個參數(shù))

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

?5.添加父視圖

[self.window addSubview:btn];

?按鈕文字

?參數(shù)1: 標(biāo)題內(nèi)容

?參數(shù)2: 狀態(tài)

[btn setTitle:@"正常" forState:UIControlStateNormal];

UITextField

創(chuàng)建UITextField

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

設(shè)置背景顏色

textField.backgroundColor = [UIColor whiteColor];

添加父視圖

[self.window addSubview:textField];

內(nèi)存管理

[textField release];

?UITextField文本控制

占位字符串 placeholder

textField.placeholder = @"紅紅火火恍恍惚惚";

UITextField輸入控制

是否可用 enabled

textField.enabled = YES;

安全文本輸入 secureTextEntry

textField.secureTextEntry = YES;

鍵盤樣式keyboardType

textField.keyboardType = UIKeyboardTypeDefault;

return(回車按鍵)樣式

textField.returnKeyType = UIReturnKeyGoogle;

開始輸入時清空

textField.text = @"輸入的內(nèi)容";

textField.clearsOnBeginEditing = YES;


UITextField外觀控制

輸入框樣式

textField.borderStyle = UITextBorderStyleNone;

?邊框?qū)挾?/p>

textField.layer.borderWidth = 1;

邊框顏色

textField.layer.borderColor = [UIColor lightGrayColor].CGColor;

切圓角(圓形: 正方形邊長的一半)

textField.layer.cornerRadius = textField.frame.size.width / 2;

?清除按鈕

textField.clearButtonMode = UITextFieldViewModeAlways;

鍵盤回收

1.簽訂系統(tǒng)協(xié)議

@interface AppDelegate () <UITextFieldDelegate>

@end

UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];

tf1.backgroundColor = [UIColor yellowColor];

[self.window addSubview:tf1];

[tf1 release];

2.設(shè)置代理人

tf1.delegate = self;

tf1.tag = 1000;

3.協(xié)議方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

? ? ? UITextField *tf1 = [self.window viewWithTag:1000];

? ? 失去第一響應(yīng)者

? ? [tf1 resignFirstResponder];

}

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

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

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