UI基礎控件篇
創建window
1.刪除Main
2.ARC->MRC(自動改手動)
3.刪除(ViewController.h/.m)
4.strong->retain(AppDelegate.h)
5.重寫dealloc方法
- (void)dealloc
{
[_windowrelease];
[superdealloc];
}
創建window和當前屏幕一樣大[UIScreen mainScreen].bounds
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
設置window顏色
self.window.backgroundColor = [UIColor whiteColor];
設置window可見
[self.window makeKeyAndVisible];
xcode7崩潰解決
self.window.rootViewController = [[UIViewController alloc] init];
內存管理
[_window release];
UIView(視圖基類)
1.創建視圖 設置frame
UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
2.設置屬性
firstView.backgroundColor = [UIColor redColor];
3.添加到父視圖(window)顯示
[self.window addSubview:firstView];
4.內存管理
[firstView release];
view屬性
顯示/隱藏hidden
aView.hidden = NO;
透明度alpha(0-1的浮點數)
aView.alpha = 1;
動畫
[UIView animateWithDuration:1 animations:^{
aView.alpha = 0;
aView.frame = CGRectMake(0, 0, 200, 200);
}];
標記值tag
aView.tag = 1000;
通過標記尋找視圖
UIView *tempView = [self.window viewWithTag:1000];
tempView.backgroundColor = [UIColor purpleColor];
定時器NSTimer
每隔一段時間 讓某某做某事
參數1:時間間隔
參數2:某某
參數3:某事
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(suibian) userInfo:nil repeats:YES];
UILabel
1.創建+frame
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
2.屬性設置
label.backgroundColor = [UIColor yellowColor];
3.添加父視圖
[self.window addSubview:label];
4.內存管理
[label release];
label文字相關屬性
顯示文字( 默認 左對齊/居中顯示/文本黑色/行數為1/背景透明色)
label.text = @"這是一個label";
文本顏色textColor
label.textColor = [UIColor purpleColor];
文本對齊方式
label.textAlignment = NSTextAlignmentCenter;
斷行模式(文本省略方式) lineBreakMode
label.lineBreakMode = NSLineBreakByTruncatingMiddle;
文本行數numberOfLines( 默認為1為0時行數不限)
label.numberOfLines = 0;
字體font
label.font = [UIFont systemFontOfSize:30];
陰影shadow
label.shadowColor = [UIColor blueColor];
陰影偏移量
label.shadowOffset = CGSizeMake(10, 10);
UIButton
1.創建(便利構造器方法)
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
2.設置frame
btn.frame = CGRectMake(100, 100, 100, 100);
3.設置屬性
btn.backgroundColor = [UIColor redColor];
4.綁定按鈕點擊事件(按鈕被點擊時 能夠觸發一個方法)
參數1: target目標(調用方法的人)
參數2: action動作(調用的方法)
參數3: events事件(方法的觸發條件)
UIControlEventTouchUpInside控制事件之 觸摸頂端按下去
“ :”參數標志(之后一定會跟隨一個參數)
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
5.添加父視圖
[self.window addSubview:btn];
按鈕文字
參數1:標題內容
參數2:狀態
[btn setTitle:@"正常" forState:UIControlStateNormal];
UITextField
創建UITextField
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
設置背景顏色
textField.backgroundColor = [UIColor whiteColor];
添加父視圖
[self.window addSubview:textField];
內存管理
[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 = @"輸入的內容";
textField.clearsOnBeginEditing = YES;
UITextField外觀控制
輸入框樣式
textField.borderStyle = UITextBorderStyleNone;
邊框寬度
textField.layer.borderWidth = 1;
邊框顏色
textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
切圓角(圓形:正方形邊長的一半)
textField.layer.cornerRadius = textField.frame.size.width / 2;
清除按鈕
textField.clearButtonMode = UITextFieldViewModeAlways;
鍵盤回收
1.簽訂系統協議
@interface AppDelegate ()
@end
UITextField *tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
tf1.backgroundColor = [UIColor yellowColor];
[self.window addSubview:tf1];
[tf1 release];
2.設置代理人
tf1.delegate = self;
tf1.tag = 1000;
3.協議方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
UITextField *tf1 = [self.window viewWithTag:1000];
失去第一響應者
[tf1 resignFirstResponder];
}