以下把 UIKit 中幾個高頻使用的控制元件,用可直接複製的程式碼整理成一份速查表,涵蓋建立方式、常用屬性與事件綁定。
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
[self.view addSubview:sw];
sw.on = NO; // 預設狀態
sw.onTintColor = [UIColor purpleColor]; // 開啟時的底色
// sw.thumbTintColor = [UIColor yellowColor]; // 圓鈕顏色
// sw.tintColor = [UIColor greenColor]; // 邊框顏色
// [sw setOnImage:[UIImage imageNamed:@"on.png"]];
// [sw setOffImage:[UIImage imageNamed:@"off.png"]];
[sw addTarget:self
action:@selector(switchBackGround:)
forControlEvents:UIControlEventValueChanged];
對應方法:
- (void)switchBackGround:(UISwitch *)sw {
self.view.backgroundColor = sw.on ? [UIColor whiteColor] : [UIColor blackColor];
}
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, 70, 250, 0)];
[self.view addSubview:slider];
slider.minimumValue = 0;
slider.maximumValue = 100;
slider.value = 50;
slider.continuous = YES; // 拖曳時連續觸發事件
slider.minimumValueImage = [UIImage imageNamed:@"min.png"];
slider.maximumValueImage = [UIImage imageNamed:@"max.png"];
slider.maximumTrackTintColor = [UIColor redColor];
slider.minimumTrackTintColor = [UIColor greenColor];
slider.thumbTintColor = [UIColor clearColor];
[slider setThumbImage:[UIImage imageNamed:@"thumb.png"] forState:UIControlStateNormal];
[slider addTarget:self
action:@selector(sliderChange:)
forControlEvents:UIControlEventValueChanged];
若設為 continuous = NO,事件只會在放開手指時觸發一次,適合節省不必要的運算。
UIImage *image = [UIImage imageNamed:@"tab.png"];
UISegmentedControl *sc =
[[UISegmentedControl alloc] initWithItems:@[@"aaa", (id)image, @"ccc", @"ddd"]];
sc.frame = CGRectMake(10, 120, 300, 40);
[self.view addSubview:sc];
sc.segmentedControlStyle = UISegmentedControlStyleBar;
// sc.momentary = YES; // 點下去後不保持選取
sc.selectedSegmentIndex = 0;
[sc insertSegmentWithTitle:@"haha" atIndex:sc.numberOfSegments animated:YES];
// [sc removeSegmentAtIndex:0 animated:YES];
// [sc setEnabled:NO forSegmentAtIndex:2];
[sc setBackgroundImage:[UIImage imageNamed:@"bg.png"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
[sc addTarget:self
action:@selector(segmentChange:)
forControlEvents:UIControlEventValueChanged];
UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(30, 180, 50, 20)];
[self.view addSubview:stepper];
stepper.minimumValue = 10;
stepper.maximumValue = 50;
stepper.stepValue = 0.01;
stepper.wraps = YES; // 到端點後是否循環
stepper.autorepeat = NO; // 長按是否持續增值
stepper.continuous = YES; // 長按過程是否即時更新數值
[stepper addTarget:self
action:@selector(stepperChange:)
forControlEvents:UIControlEventValueChanged];
步進器通常搭配一個 label 顯示目前值,在 stepperChange: 中同步更新即可。
progress = [[UIProgressView alloc] initWithFrame:CGRectMake(10, 280, 300, 30)];
[self.view addSubview:progress];
progress.progressViewStyle = UIProgressViewStyleBar;
progress.progressTintColor = [UIColor greenColor];
progress.trackTintColor = [UIColor redColor];
progress.progress = 0.0;
[NSTimer scheduledTimerWithTimeInterval:0.05
target:self
selector:@selector(refresh)
userInfo:nil
repeats:YES];
- (void)refresh {
if (progress.progress < 1.0) {
[progress setProgress:(progress.progress + 0.01) animated:YES];
} else {
[timer invalidate];
}
}
UIActivityIndicatorView *activity =
[[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activity.frame = CGRectMake(10, 330, 45, 45);
activity.hidesWhenStopped = YES;
activity.color = [UIColor yellowColor];
[self.view addSubview:activity];
[activity startAnimating];
// [activity stopAnimating];
UITextView *text = [[UITextView alloc] initWithFrame:CGRectMake(10, 350, 300, 80)]; text.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:text]; // 自訂鍵盤或鍵盤上的附加列 UIView *accessory = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)]; accessory.backgroundColor = [UIColor blueColor]; text.inputAccessoryView = accessory; // 加在鍵盤上方 // text.inputView = accessory; // 完全取代鍵盤
// 警示框
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"標題"
message:@"你喜歡我嗎"
delegate:self
cancelButtonTitle:@"喜歡"
otherButtonTitles:@"很喜歡", @"超級喜歡", nil];
[alert show];
// 動作表
sheet = [[UIActionSheet alloc] initWithTitle:@"請選擇"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:@"從相機拍攝"
otherButtonTitles:@"從相簿選取", nil];
[sheet showInView:self.view];
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"alert 按鈕 index = %d", buttonIndex);
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"sheet 按鈕 index = %d", buttonIndex);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITextView class]]) {
[(UITextView *)view resignFirstResponder];
}
}
}
這份速查表中的 UIAlertView、UIActionSheet 與 UISegmentedControlStyle 皆已在新版 iOS 中被標記為 deprecated。新專案請改用 UIAlertController(以 action 物件取代 delegate 回呼,程式碼更清晰),分段控制器也不再需要設定 style。上面這些寫法保留下來,主要是為了看懂與維護既有專案。