現(xiàn)下大部分的UI設(shè)計(jì)都是以TableView為主,但仍需要用一些控件來(lái)優(yōu)化結(jié)構(gòu),增加功能。本文為大家介紹一些常用的控件和常見的基本用法。
相關(guān)文章:
TextField相關(guān)基礎(chǔ)用法
若干搭建UI常用的小控件(二)
UISwitch
UISwitch是比較常用的控件,在設(shè)置界面應(yīng)用較多。通常情況下定制較少,大部分定制的情況都是在定制顏色上面。
UISwitch * switch = [[UISwitch alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//寬和高無(wú)用,注意center
//如果確實(shí)想要更改大小,可以通過繼承于UIView來(lái)修改transform
設(shè)置開關(guān)的顏色
switch.onTintColor = [UIColor redColor];//打開狀態(tài)下的顏色
switch.tintColor = [UIColor blackColor];//關(guān)閉狀態(tài)下的鏤空色
switch.thumbTintColor = [UIColor yellowColor]; //滑塊顏色
設(shè)置當(dāng)前開關(guān)的狀態(tài)
switch.on = YES;
[switch addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
對(duì)控件添加時(shí)間,繼承自UIControl的控件都能通過添事件變成Trigger
- (void)setOn:(BOOL)on animated:(BOOL)animated;//帶動(dòng)畫效果的開關(guān),需要被某個(gè)觸發(fā)器觸發(fā)
UIActionSheet
這也是個(gè)十分常用的控件,默認(rèn)是不會(huì)顯示的,只有被觸發(fā)之后,才會(huì)顯示。常用于完成任務(wù)的不同方法,無(wú)需定制。
協(xié)議為UIActionSheetDelegate,在使用之前需要添加。、
初始化
UIActionSheet * actionSheet = [[UIActionSheet alloc]
initWithTitle:@"分享"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"微博", @"微信好友", @"朋友圈", nil];
風(fēng)格樣式
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
UIActionSheetStyleAutomatic //白色
UIActionSheetStyleDefault //白色
UIActionSheetStyleBlackTranslucent //黑色透明
UIActionSheetStyleBlackOpaque //黑色不透明
顯示風(fēng)格
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;
//可定制顯示風(fēng)格
- (void)showInView:(UIView *)view;
//默認(rèn)顯示風(fēng)格
協(xié)議方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//獲取點(diǎn)擊事件的位置,執(zhí)行其他操作
{
switch (buttonIndex) {
case 0: {
break;
}
case 1: {
break;
}
default:
break;
}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
//執(zhí)行關(guān)閉操作
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
//即將顯示,在動(dòng)畫之前
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
//即將顯示,在動(dòng)畫之后
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
// 將要關(guān)閉, 點(diǎn)擊了buttonIndex
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
//已經(jīng)關(guān)閉, 點(diǎn)擊了buttonIndex]);
UIAlertView
UIAlertView常用語(yǔ)輸入密碼、警告等場(chǎng)景,也是需要觸發(fā)之后才會(huì)顯現(xiàn)出來(lái)的一個(gè)控件(類似UIActionSheet),這是一個(gè)不需要定制的控件,如需定制,可以自建UIView進(jìn)行定制。
協(xié)議為UIAlertViewDelegate。
初始化
UIAlertView * alertView = [[UIAlertView alloc]
initWithTitle:@"警告"
message:@"message"
delegate:self
cancelButtonTitle:@"放棄"
otherButtonTitles:@"1", @"2", @"3", nil];
//alertView不需要添加到父視圖,系統(tǒng)直接將其添加到window上,以上每個(gè)參數(shù),如不需要,都可以傳nil
//Title是標(biāo)題,顯示警告的主體內(nèi)容。
//message是警告的具體信息,用以描述警告的內(nèi)容。
//cancelButtonTitle是取消鍵的title
//otherButtonTitles是其他鍵的title,可以賦值為nil
顯示風(fēng)格
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UIAlertViewStyleDefault //默認(rèn)
UIAlertViewStyleSecureTextInput //密文textField
UIAlertViewStylePlainTextInput //平鋪textFiled
UIAlertViewStyleLoginAndPasswordInput //用戶名密碼
顯示
[alertView show];//觸發(fā)顯示UIAlertView之后,需要執(zhí)行這段代碼,如沒有,則不顯示
協(xié)議方法
- (void)alertViewCancel:(UIAlertView *)alertView//被取消之后執(zhí)行
- (void)willPresentAlertView:(UIAlertView *)alertView//即將顯示,在動(dòng)畫之前
- (void)didPresentAlertView:(UIAlertView *)alertView//已經(jīng)顯示,在動(dòng)畫之后
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
//取消鍵0,其他鍵從1開始,即將消失,動(dòng)畫之前,點(diǎn)擊了buttonIndex號(hào)按鈕
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
//已經(jīng)消失,動(dòng)畫之后,點(diǎn)擊了buttonIndex號(hào)按鈕
另
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
//第一個(gè)其他按鈕是否有效,當(dāng)textField上文字發(fā)生改變
{
//獲取alertView上的textField
UITextField * textField = [alertView textFieldAtIndex:1];
//比如輸入用戶名密碼,可以判斷如果密碼不夠6位,用戶名不夠6位,不許登陸
if (tf.text.length != 6)
return NO;
return YES;
}
很多的APP開發(fā)過程當(dāng)中,經(jīng)常是應(yīng)該使用UIAlertView來(lái)代替UIActionSheet的功能來(lái)使用,雖然說功能是現(xiàn)實(shí)方面并不會(huì)出現(xiàn)巨大的問題,但是事實(shí)上這種使用方法確實(shí)不值得提倡的。如此會(huì)造成用戶的使用疲勞,使用戶的使用感受大打折扣。
UIAlertView 作為一個(gè)占據(jù)屏幕中央的臨時(shí)模態(tài)層,會(huì)嚴(yán)重打斷用戶的當(dāng)前任務(wù)與操作,吸引用戶所有注意力。因此,Apple 官方也嚴(yán)肅地建議:盡量避免使用。
UITextView
UITextView繼承于UIScrollView,UIScrollView是UIView的子類,所以,UITextView繼承以上的一些方法。
協(xié)議為UITextViewDelegate
textView 是滾動(dòng)視圖的子類,滾動(dòng)視圖,如果檢測(cè)到當(dāng)前視圖控制器在導(dǎo)航控制器下,會(huì)自動(dòng)留出導(dǎo)航控制器的高度。因?yàn)閟crollView很多時(shí)候是全屏顯示的。
修復(fù)方法如下:
self.automaticallyAdjustsScrollViewInsets = NO;
//創(chuàng)建
UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 70, 300, 200)];
textView.backgroundColor = [UIColor orangeColor];
//設(shè)置字體
textView.font = [UIFont systemFontOfSize:30];
//設(shè)置字體顏色
textView.textColor = [UIColor blueColor];
//設(shè)置字體的對(duì)齊方式
textView.textAlignment = NSTextAlignmentLeft;
//設(shè)置能否滾動(dòng)
textView.scrollEnabled = NO;
//設(shè)置鍵盤色彩
textView.keyboardAppearance = UIKeyboardAppearanceAlert;
UIKeyboardAppearanceDark //黑色
UIKeyboardAppearanceLight //白色
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark
//設(shè)置鍵盤樣式
textView.keyboardType = UIKeyboardTypeDefault;//默認(rèn)樣式
//設(shè)置鍵盤的return鍵,僅在鍵盤樣式default生效
textView.returnKeyType = UIReturnKeyDefault;//默認(rèn)樣式
//設(shè)置是否自動(dòng)大小寫
textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//設(shè)置是否自動(dòng)糾錯(cuò)
textView.autocorrectionType = UITextAutocorrectionTypeNo;
UITextAutocorrectionTypeDefault //默認(rèn)
UITextAutocorrectionTypeNo //不自動(dòng)糾錯(cuò)
UITextAutocorrectionTypeYes //自動(dòng)糾錯(cuò)
//設(shè)置彈出自定義鍵盤
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 100)];
view.backgroundColor = [UIColor greenColor];
textView.inputView = view;
//設(shè)置自定義二級(jí)鍵盤
textView.inputAccessoryView = nil;
textView.inputView = nil;
//設(shè)置禁止編輯
textView.editable = YES;
//設(shè)置默認(rèn)文字
textView.text = @"默認(rèn)文字";
//設(shè)置不能進(jìn)行任何操作
textView.userInteractionEnabled = NO;
關(guān)于鍵盤的設(shè)置,我會(huì)在另一篇中寫出。