UI基礎(chǔ)控件篇
創(chuàng)建window
1.刪除Main
2.ARC->MRC(自動(dòng)改手動(dòng))
3.刪除(ViewController.h/.m)
4.strong->retain(AppDelegate.h)
5.重寫dealloc方法
- (void)dealloc
{
[_windowrelease];
[superdealloc];
}
創(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可見(jiàn)
[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的浮點(diǎn)數(shù))
aView.alpha = 1;
動(dòng)畫
[UIView animateWithDuration:1 animations:^{
aView.alpha = 0;
aView.frame = CGRectMake(0, 0, 200, 200);
}];
標(biāo)記值tag
aView.tag = 1000;
通過(guò)標(biāo)記尋找視圖
UIView *tempView = [self.window viewWithTag:1000];
tempView.backgroundColor = [UIColor purpleColor];
定時(shí)器NSTimer
每隔一段時(shí)間 讓某某做某事
參數(shù)1:時(shí)間間隔
參數(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)屬性
顯示文字( 默認(rèn) 左對(duì)齊/居中顯示/文本黑色/行數(shù)為1/背景透明色)
label.text = @"這是一個(gè)label";
文本顏色textColor
label.textColor = [UIColor purpleColor];
文本對(duì)齊方式
label.textAlignment = NSTextAlignmentCenter;
斷行模式(文本省略方式) lineBreakMode
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
文本行數(shù)numberOfLines( 默認(rèn)為1為0時(shí)行數(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.綁定按鈕點(diǎn)擊事件(按鈕被點(diǎn)擊時(shí) 能夠觸發(fā)一個(gè)方法)
參數(shù)1: target目標(biāo)(調(diào)用方法的人)
參數(shù)2: action動(dòng)作(調(diào)用的方法)
參數(shù)3: events事件(方法的觸發(fā)條件)
UIControlEventTouchUpInside控制事件之 觸摸頂端按下去
“ :”參數(shù)標(biāo)志(之后一定會(huì)跟隨一個(gè)參數(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;
開始輸入時(shí)清空
textField.text = @"輸入的內(nèi)容";
textField.clearsOnBeginEditing = YES;
UITextField外觀控制
輸入框樣式
textField.borderStyle = UITextBorderStyleNone;
邊框?qū)挾?/p>
textField.layer.borderWidth = 1;
邊框顏色
textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
切圓角(圓形:正方形邊長(zhǎng)的一半)
textField.layer.cornerRadius = textField.frame.size.width / 2;
清除按鈕
textField.clearButtonMode = UITextFieldViewModeAlways;
鍵盤回收
1.簽訂系統(tǒng)協(xié)議
@interface AppDelegate ()
@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];
}