現(xiàn)下大部分的UI設(shè)計都是以TableView為主苞轿,但仍需要用一些控件來優(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)];
//寬和高無用负懦,注意center
//如果確實想要更改大小筒捺,可以通過繼承于UIView來修改transform
設(shè)置開關(guān)的顏色
switch.onTintColor = [UIColor redColor];//打開狀態(tài)下的顏色
switch.tintColor = [UIColor blackColor];//關(guān)閉狀態(tài)下的鏤空色
switch.thumbTintColor = [UIColor yellowColor]; //滑塊顏色
設(shè)置當前開關(guān)的狀態(tài)
switch.on = YES;
[switch addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
對控件添加時間,繼承自UIControl的控件都能通過添事件變成Trigger
- (void)setOn:(BOOL)on animated:(BOOL)animated;//帶動畫效果的開關(guān)纸厉,需要被某個觸發(fā)器觸發(fā)
UIActionSheet
這也是個十分常用的控件系吭,默認是不會顯示的,只有被觸發(fā)之后颗品,才會顯示肯尺。常用于完成任務(wù)的不同方法,無需定制躯枢。
協(xié)議為UIActionSheetDelegate则吟,在使用之前需要添加。锄蹂、
初始化
UIActionSheet * actionSheet = [[UIActionSheet alloc]
initWithTitle:@"分享"
delegate:self
cancelButtonTitle:@"取消"
destructiveButtonTitle:nil
otherButtonTitles:@"微博", @"微信好友", @"朋友圈", nil];
風格樣式
actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
UIActionSheetStyleAutomatic //白色
UIActionSheetStyleDefault //白色
UIActionSheetStyleBlackTranslucent //黑色透明
UIActionSheetStyleBlackOpaque //黑色不透明
顯示風格
- (void)showFromRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated;
//可定制顯示風格
- (void)showInView:(UIView *)view;
//默認顯示風格
協(xié)議方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//獲取點擊事件的位置氓仲,執(zhí)行其他操作
{
switch (buttonIndex) {
case 0: {
break;
}
case 1: {
break;
}
default:
break;
}
}
- (void)actionSheetCancel:(UIActionSheet *)actionSheet
//執(zhí)行關(guān)閉操作
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
//即將顯示,在動畫之前
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
//即將顯示得糜,在動畫之后
- (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex
// 將要關(guān)閉, 點擊了buttonIndex
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
//已經(jīng)關(guān)閉, 點擊了buttonIndex]);
UIAlertView
UIAlertView常用語輸入密碼敬扛、警告等場景,也是需要觸發(fā)之后才會顯現(xiàn)出來的一個控件(類似UIActionSheet)朝抖,這是一個不需要定制的控件舔哪,如需定制,可以自建UIView進行定制槽棍。
協(xié)議為UIAlertViewDelegate。
初始化
UIAlertView * alertView = [[UIAlertView alloc]
initWithTitle:@"警告"
message:@"message"
delegate:self
cancelButtonTitle:@"放棄"
otherButtonTitles:@"1", @"2", @"3", nil];
//alertView不需要添加到父視圖抬驴,系統(tǒng)直接將其添加到window上炼七,以上每個參數(shù),如不需要布持,都可以傳nil
//Title是標題豌拙,顯示警告的主體內(nèi)容。
//message是警告的具體信息题暖,用以描述警告的內(nèi)容按傅。
//cancelButtonTitle是取消鍵的title
//otherButtonTitles是其他鍵的title,可以賦值為nil
顯示風格
alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
UIAlertViewStyleDefault //默認
UIAlertViewStyleSecureTextInput //密文textField
UIAlertViewStylePlainTextInput //平鋪textFiled
UIAlertViewStyleLoginAndPasswordInput //用戶名密碼
顯示
[alertView show];//觸發(fā)顯示UIAlertView之后胧卤,需要執(zhí)行這段代碼唯绍,如沒有,則不顯示
協(xié)議方法
- (void)alertViewCancel:(UIAlertView *)alertView//被取消之后執(zhí)行
- (void)willPresentAlertView:(UIAlertView *)alertView//即將顯示枝誊,在動畫之前
- (void)didPresentAlertView:(UIAlertView *)alertView//已經(jīng)顯示况芒,在動畫之后
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
//取消鍵0,其他鍵從1開始叶撒,即將消失绝骚,動畫之前耐版,點擊了buttonIndex號按鈕
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
//已經(jīng)消失,動畫之后压汪,點擊了buttonIndex號按鈕
另
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
//第一個其他按鈕是否有效粪牲,當textField上文字發(fā)生改變
{
//獲取alertView上的textField
UITextField * textField = [alertView textFieldAtIndex:1];
//比如輸入用戶名密碼,可以判斷如果密碼不夠6位止剖,用戶名不夠6位腺阳,不許登陸
if (tf.text.length != 6)
return NO;
return YES;
}
很多的APP開發(fā)過程當中,經(jīng)常是應(yīng)該使用UIAlertView來代替UIActionSheet的功能來使用滴须,雖然說功能是現(xiàn)實方面并不會出現(xiàn)巨大的問題舌狗,但是事實上這種使用方法確實不值得提倡的。如此會造成用戶的使用疲勞扔水,使用戶的使用感受大打折扣痛侍。
UIAlertView 作為一個占據(jù)屏幕中央的臨時模態(tài)層,會嚴重打斷用戶的當前任務(wù)與操作魔市,吸引用戶所有注意力主届。因此,Apple 官方也嚴肅地建議:盡量避免使用待德。
UITextView
UITextView繼承于UIScrollView君丁,UIScrollView是UIView的子類,所以将宪,UITextView繼承以上的一些方法绘闷。
協(xié)議為UITextViewDelegate
textView 是滾動視圖的子類,滾動視圖较坛,如果檢測到當前視圖控制器在導(dǎo)航控制器下印蔗,會自動留出導(dǎo)航控制器的高度。因為scrollView很多時候是全屏顯示的丑勤。
修復(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è)置字體的對齊方式
textView.textAlignment = NSTextAlignmentLeft;
//設(shè)置能否滾動
textView.scrollEnabled = NO;
//設(shè)置鍵盤色彩
textView.keyboardAppearance = UIKeyboardAppearanceAlert;
UIKeyboardAppearanceDark //黑色
UIKeyboardAppearanceLight //白色
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark
//設(shè)置鍵盤樣式
textView.keyboardType = UIKeyboardTypeDefault;//默認樣式
//設(shè)置鍵盤的return鍵华嘹,僅在鍵盤樣式default生效
textView.returnKeyType = UIReturnKeyDefault;//默認樣式
//設(shè)置是否自動大小寫
textView.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//設(shè)置是否自動糾錯
textView.autocorrectionType = UITextAutocorrectionTypeNo;
UITextAutocorrectionTypeDefault //默認
UITextAutocorrectionTypeNo //不自動糾錯
UITextAutocorrectionTypeYes //自動糾錯
//設(shè)置彈出自定義鍵盤
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 100)];
view.backgroundColor = [UIColor greenColor];
textView.inputView = view;
//設(shè)置自定義二級鍵盤
textView.inputAccessoryView = nil;
textView.inputView = nil;
//設(shè)置禁止編輯
textView.editable = YES;
//設(shè)置默認文字
textView.text = @"默認文字";
//設(shè)置不能進行任何操作
textView.userInteractionEnabled = NO;
關(guān)于鍵盤的設(shè)置,我會在另一篇中寫出法竞。