UI常用控件

UICommonlyUsedControls

【UI常用控件】

不需要学习多么深入,但是要知道系统提供的有用的控件。

一、UISwitch(开关)

二、UIActivityIndicatorView(活动指示视图)

三、UISlider(滑动条)

四、UIProgressView(进度条)

五、UIStepper(步进器)

六、UISegmentedControl(分段控制)

七、UIActionSheet(操作表单)

八、UIAlertView(警告视图)  [alertView show]

九、UITextView(文本视图)

一、UISwitch(开关)

#pragma mark - 开关

- (void)createUISwitch {

UISwitch *sw = [[UISwitch alloc]init];

//开关的宽高没有影响

sw.frame = CGRectMake(10, 100, 0, 0);

//用形变transform 可以修改大小

sw.transform = CGAffineTransformMakeScale(1.5, 1.5);

[self.view addSubview:sw];

//添加事件

[sw addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];

//设置边框颜色

sw.tintColor = [UIColor blueColor];

//设置打开的颜色

sw.onTintColor = [UIColor purpleColor];

//设置小球的颜色

sw.thumbTintColor = [UIColor greenColor];

}

- (void)switchChange:(UISwitch*)sw {

if (sw.on == YES) {

NSLog(@"开着");

}

else{

NSLog(@"关闭");

}

}

二、UIActivityIndicatorView(活动指示视图)

//状态栏上边的网络加载指示器

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

- (void)createView {

//    UIActivityIndicatorViewStyleWhiteLarge,大白块

//    UIActivityIndicatorViewStyleWhite,白色

//    UIActivityIndicatorViewStyleGray,灰色

UIActivityIndicatorView *avi = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

avi.frame = CGRectMake(0, 0, 50, 50);

avi.center = self.view.center;

avi.tag = 100;

[self.view addSubview:avi];

[avi startAnimating];

}

//触摸屏幕时让指示器停止

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

[super touchesBegan:touches withEvent:event];

UIActivityIndicatorView *avi = (id)[self.view viewWithTag:100];

[avi stopAnimating];

//关闭状态栏上边的网络加载指示器

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

}

三、UISlider(滑动条)

- (void)createView {

UISlider *sl = [[UISlider alloc]init];

//高度不生效

sl.frame = CGRectMake(100, 100, 300, 100);

[self.view addSubview:sl];

//设置颜色

//运行过的滑动条的颜色

sl.minimumTrackTintColor = [UIColor greenColor];

//刚开始滑动条的颜色

sl.maximumTrackTintColor = [UIColor redColor];

//球球的颜色

sl.thumbTintColor = [UIColor purpleColor];

//设置值

//球球在滑动条上的位置,不设置默认0;

sl.minimumValue = 0.3;

sl.maximumValue = 1.0;

//初始值  球球在滑动条上的初始位置

sl.value = 0.3;

[sl addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];

//是否持续调用事件  默认YES,在滑动的过程中一直改变值 ,NO只有在滑动停止的时候才改变值

sl.continuous = NO;

}

- (void)changeValue:(UISlider*)sender{

NSLog(@"%2f",sender.Value);

}

四、UIProgressView(进度条)

- (void)createView {

//    UIProgressViewStyleDefault,     // normal progress bar

//    UIProgressViewStyleBar,

UIProgressView *pv = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];

//高度无效

pv.frame = CGRectMake(20, 100, 300, 100);

[self.view addSubview:pv];

//设置进度条颜色

pv.trackTintColor = [UIColor greenColor];

//进度条走过的颜色

pv.progressTintColor = [UIColor redColor];

//进度

pv.progress = 0.1;

[self autoChange:pv];

// [pv setProgress:0.8 animated:YES];

}

//自动增加

- (void)autoChange:(UIProgressView*)pv {

CGFloat progress = pv.progress;

progress +=0.001;

[pv setProgress:progress animated:YES];

if (progress >= 1.0) {

return;

}

//延迟调用一个方法

[self performSelector:@selector(autoChange:) withObject:pv afterDelay:0.01];

}

五、UIStepper(步进器)

//步进器

- (void)createView {

UIStepper *sp = [[UIStepper alloc]init];

//宽高无效

sp.frame = CGRectMake(20, 100, 200, 100);

[self.view addSubview:sp];

//添加事件

[sp addTarget:self action:@selector(stepperChange:) forControlEvents:UIControlEventValueChanged];

//设置值

sp.minimumValue = 5;

sp.maximumValue = 100;

//一次加多少

sp.stepValue = 5;

sp.tintColor = [UIColor yellowColor];

//设置跨越边界,默认不能跨越

sp.wraps = NO;

//长按是否持续加减

sp.autorepeat = NO;

//是否持续调用事件

sp.continuous = NO;

}

- (void)stepperChange:(UIStepper*)sp {

NSLog(@"%.2f",sp.value);

}

六、UISegmentedControl(分段控制)

- (void)createView {

NSArray *items = @[@"好友",@"消息",@"动态"];

UISegmentedControl *sc = [[UISegmentedControl alloc]initWithItems:items];

sc.frame = CGRectMake(100, 100, 200, 50);

[self.view addSubview:sc];

//设置颜色

sc.backgroundColor = [UIColor greenColor];

sc.tintColor = [UIColor redColor];

//设置字体大小和颜色(对应状态)

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor blueColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:20],NSFontAttributeName, nil];

[sc setTitleTextAttributes:dic forState:UIControlStateSelected];

//添加事件

[sc addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];

}

- (void)changeValue:(UISegmentedControl*)sender {

NSLog(@"%ld",sender.selectedSegmentIndex);

//事件分发

}

七、UIActionSheet(操作表单)

/**

*  ios8以后 新的actionSheet 和alertView 和在一起

*/

- (void)createAlertViewController {

//    UIAlertControllerStyleActionSheet = 0,

//    UIAlertControllerStyleAlert

UIAlertController *alc= [UIAlertController alertControllerWithTitle:@"标题" message:@"信息" preferredStyle:UIAlertControllerStyleActionSheet];

//添加按钮

//    UIAlertActionStyleDefault = 0,

//    UIAlertActionStyleCancel,

//    UIAlertActionStyleDestructive

//没有设置代理,用block替换了代理

UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"确定按钮点击");

}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertAction *desc = [UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

}];

//添加进去

[alc addAction:action];

[alc addAction:cancel];

[alc addAction:desc];

[self presentViewController:alc animated:YES completion:nil];

}

- (void)createView {

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"desc" otherButtonTitles:@"其他1",@"其他2", nil];

actionSheet.tag = 100;

//显示

[actionSheet showInView:self.view];

}

//点击了哪个Button

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

NSLog(@"%ld",buttonIndex);

//这里边做事件分发

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

[super touchesBegan:touches withEvent:event];

//    UIActionSheet *ac = (id)[self.view viewWithTag:100];

//    [ac showInView:self.view];

[self createView];

}

八、UIAlertView(警告视图)

@interface ViewController7 ()<UIAlertViewDelegate>

@end

- (void)createView {

UIAlertView *al = [[UIAlertView alloc]initWithTitle:@"标题" message:@"你好" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",@"更新", nil];

//显示

[al show];

}

//点击按钮的代理

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

NSLog(@"%ld",buttonIndex);

if (alertView.tag == 100) {

UIAlertView *al1 = [[UIAlertView alloc]initWithTitle:@"另一个alert" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

al1.tag = 101;

//        UIAlertViewStyleDefault = 0,

//        UIAlertViewStyleSecureTextInput,安全输入

//        UIAlertViewStylePlainTextInput,普通输入

//        UIAlertViewStyleLoginAndPasswordInput 两个输入框

al1.alertViewStyle = UIAlertViewStylePlainTextInput;

UITextField *textf = [al1 textFieldAtIndex:0];

textf.placeholder = @"请输入用户名";

[al1 show];

}

else {

NSLog(@"点了另一个alertView");

UITextField *textfeild = [alertView textFieldAtIndex:0];

NSLog(@"输入了%@",textfeild.text);

}

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

[super touchesBegan:touches withEvent:event];

[self createView];

}

九、UITextView(文本视图)

- (void)createTextView {

UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 100, 100, 300)];

[self.view addSubview:textView];

//取消导航对布局的影响

self.edgesForExtendedLayout = UIRectEdgeNone;

//

textView.backgroundColor = [UIColor orangeColor];

textView.textColor = [UIColor purpleColor];

textView.font = [UIFont systemFontOfSize:30];

//取消滑动

textView.scrollEnabled = NO;

//    textView.text = @"jdfjdjglsdfjgjdfsljgsldfjhklsjglkjfdlkjhmlksfjhkllljmfdotjrkjhodkrtoghtrgkhotrkgodrtkgkdtrkgdotkrgoktrgokrtgtrh";

//    //设置不可编辑(必须text里边有内容)

//    textView.editable = NO;

//代理方法 设置代理

textView.delegate = self;

}

//实现代理方法

//是否允许开始编辑

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {

NSLog(@"开始编辑");

return YES;

}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {

NSLog(@"结束编辑");

return YES;

}

- (void)textViewDidBeginEditing:(UITextView *)textView {

NSLog(@"已经开始编辑");

}

- (void)textViewDidChange:(UITextView *)textView {

NSLog(@"已经改变");

}

- (void)textViewDidChangeSelection:(UITextView *)textView {

NSLog(@"只要选中内容就会调用");

}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

//只要输入内容就会调用

NSLog(@"%@",text);

return YES;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

[super touchesBegan:touches withEvent:event];

//直接找到textView 取消第一响应

//self.view endEditing

[self.view endEditing:YES];

}

上一篇:IIS HTTP重定向配置、参数设置


下一篇:使用Vue.extend实现iview Upload在单文件上传时,拖拽多个文件给出错误提示