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