不明觉厉iOSiOS移动开发iOS 之路

若干搭建UI常用的小控件(一)

2015-09-20  本文已影响1541人  ac41d8480d04

现下大部分的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;

关于键盘的设置,我会在另一篇中写出。

上一篇下一篇

猜你喜欢

热点阅读