這份筆記系統整理了 iOS 開發中最常用的基礎知識,涵蓋 UIView、UILabel、UIButton、UIImageView、UITextField、頁面跳轉、導航控制器與工具欄等核心控件,適合初學者反覆查閱。
UIView 是所有控件的父類,通過 frame 或 center + bounds 定位。座標系原點在螢幕左上角,X 軸向右、Y 軸向下為正方向。
UIView *view = [[UIView alloc] init]; //方式一:frame view.frame = CGRectMake(50, 100, 220, 220); //方式二:center + bounds view1.center = CGPointMake(50+220/2, 100+220/2); view1.bounds = CGRectMake(0, 0, 220, 220); view.backgroundColor = [UIColor greenColor]; view.hidden = NO; //父視圖隱藏,子視圖跟著隱藏 view.alpha = 1; //父視圖透明度影響子視圖 view.tag = 1333; //可透過 tag 讀取視圖 [self.window addSubview:view];
view.layer.cornerRadius = 20; //圓角 view.layer.borderWidth = 4; //邊框寬度 view.layer.borderColor = [UIColor greenColor].CGColor; //邊框顏色 view.layer.masksToBounds = NO; //是否裁剪子視圖 view.layer.shadowColor = [UIColor grayColor].CGColor; //陰影顏色 view.layer.shadowOffset = CGSizeMake(100, 100); //陰影偏移 view.layer.shadowOpacity = 1; //陰影透明度 view.layer.shadowRadius = 50; //陰影虛化
[self.view bringSubviewToFront:view1]; //移到最上 [self.view sendSubviewToBack:view1]; //移到最下 [self.view insertSubview:view2 aboveSubview:view3]; //插到某視圖上方 [self.view insertSubview:view2 belowSubview:view3]; //插到某視圖下方 [self.view insertSubview:view1 atIndex:2]; //插到指定位置 [self.view exchangeSubviewAtIndex:2 withSubviewAtIndex:1]; //交換位置
需要注意的是,超出父視圖範圍的子視圖不會響應事件;label.userInteractionEnabled 設為 YES 時其子視圖才有響應。
label.text = @"這是一個 UILabel"; label.textColor = [UIColor orangeColor]; label.font = [UIFont systemFontOfSize:18.0]; //字體大小 label.font = [UIFont boldSystemFontOfSize:18.0]; //加粗 label.textAlignment = NSTextAlignmentLeft; //對齊方式 label.numberOfLines = 0; //0 表示自動換行 label.adjustsFontSizeToFitWidth = YES; //自適應字體 [label sizeToFit]; //自適應高度
自適應高度可透過 boundingRectWithSize 計算文字實際尺寸:
NSDictionary *fontDic = @{NSFontAttributeName: font};
CGRect labelRect = [labelStr boundingRectWithSize:CGSizeMake(300, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
label.frame = CGRectMake(10, 30, 300, labelRect.size.height);
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; btn.frame = CGRectMake(10, 60, 300, 30); [btn setTitle:@"我是按鈕" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:18.0]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
四種狀態分別為 Normal、Highlighted、Selected、Disabled。圖片按鈕需使用 UIButtonTypeCustom,並可通過 setImage 或 setBackgroundImage 設置圖片。
加載圖片有兩種方式:imageNamed: 效率高但會緩存佔內存,適合小圖;imageWithContentsOfFile: 用完即銷毀、省內存但效率低,適合大圖。
//序列幀動畫
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i<=12; i++) {
[array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"player%d", i]]];
}
imageView.animationImages = array;
imageView.animationDuration = 1; //每次循環時間
imageView.animationRepeatCount = 0; //0 表示無限循環
[imageView startAnimating];
textField.borderStyle = UITextBorderStyleRoundedRect; //邊框樣式
textField.returnKeyType = UIReturnKeyNext; //return 鍵樣式
textField.keyboardType = UIKeyboardTypeDefault; //鍵盤樣式
textField.placeholder = @"請輸入。。。"; //占位提示
textField.secureTextEntry = NO; //是否密文
textField.delegate = self;
//return 鍵點擊時收起鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
//模態跳轉 [self presentViewController:second animated:YES completion:nil]; [self dismissViewControllerAnimated:YES completion:nil]; //通過 AppDelegate 傳值 AppDelegate *app = [[UIApplication sharedApplication] delegate]; app.str = @"gggggggg";
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root]; self.window.rootViewController = nav; [self.navigationController pushViewController:first animated:YES]; //下一頁 [self.navigationController popViewControllerAnimated:YES]; //上一頁 [self.navigationController popToRootViewControllerAnimated:YES]; //回到首頁
狀態欄高度為 20,導航欄高度為 44,兩者合計 64,因此導航類頁面中控件的起始 Y 通常為 64。
self.title = @"頁面一";
self.navigationItem.titleView = label; //自定義標題視圖
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"left"
style:UIBarButtonItemStylePlain target:self action:@selector(leftButton)];
self.navigationItem.leftBarButtonItem = left;
//工具欄
self.navigationController.toolbarHidden = NO;
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
self.toolbarItems = @[space, item1, space, item2];
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:30.0],
NSForegroundColorAttributeName:[UIColor redColor]};
[[UINavigationBar appearance] setTitleTextAttributes:dic];
透過 UIAppearance,可以統一修改所有導航欄標題的字體與顏色,避免逐個設置。