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];
}
UI常用控件的更多相关文章
- UI常用控件的一些属性
UILable 1 //设置文本信息 2 nameLable.text = @"用户名:"; 3 //对齐方式(居中 居左 局右); 4 nameLable.textAlignme ...
- easy ui 常用控件配置
table comboBox 下拉高度 panelHeight:'auto' textBox
- [WinForm]WinForm跨线程UI操作常用控件类大全
前言 在C#开发的WinForm窗体程序开发的时候,经常会使用多线程处理一些比较耗时之类的操作.不过会有一个问题:就是涉及到跨线程操作UI元素. 相信才开始接触的人一定会遇上这个问题. 为了解决这个问 ...
- 【Android Studio】安卓开发初体验3.1——UI设计之常用控件
常用控件 首先对xml文件的编辑有三种模式 Code为纯代码 Split是一边代码,一边预览效果图 Designer就是有UI设计界面 TextView 用于在界面上显示一段文本信息 所有控件都可以在 ...
- Day3 UI:7种常用控件、4种基本布局
Android常用控件 TextView <TextView android:id="@+id/text_view" android:layout_width="m ...
- UWP学习记录7-设计和UI之控件和模式4
UWP学习记录7-设计和UI之控件和模式4 1.翻转视图 使用翻转视图浏览集合中的图像或其他项目(例如相册中的照片或产品详细信息页中的项目),一次显示一个项目. 对于触摸设备,轻扫某个项将在整个集合中 ...
- Xamarin Studio在Mac环境下的配置和Xamarin.iOS常用控件的示例
看过好多帖子都是Win环境装XS,Mac只是个模拟器,讲解在Mac环境下如何配置Xamarin Studio很少,也是一点点找资料,东拼西凑才把Xamarin Studio装在Mac上跑起来,如下: ...
- B/S一些小知识及常用控件
一: B/S网页的运行 页面在设计的时候,本身就是一个类.在运行的时间,是一个对象. 其中aspx和aspx.cs是在同一个类下. aspx是主要是负责界面,而aspx.cs主要是负责数据逻辑. 呈现 ...
- QMUI UI库 控件 弹窗 列表 工具类 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
随机推荐
- shell脚本 整数比较
1.整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne " ...
- Linux教程之配置权限受限制的SFTP
SFTP 在Linux下是一个很方便很安全的文件传输工具,我常常用它在Linux服务器上替代传统的ftp来传输文件.众所周知SFTP账号是基于SSH账号的,默认情况下访问服务器的权限很大,下面的教程就 ...
- Unable to chmod
不能改变权限 Unable to chmod /system/build.prop.: Read-only file system 解决方式: before chmod: Code: mount -o ...
- ios中关于UIImagePickerController的一些知识总结
记得添加MobileCoreServices.framework 及导入#import <MobileCoreServices/MobileCoreServices.h> @interfa ...
- Struts2的运行机制简介
1.客户端通过URL请求tomcat 2.URL找到对应站点的WEB.xml 发现里面有 struts2配置 3.执行StrutsPrepareAndExecuteFilter类的init方法 4 ...
- 转 [分享一个SQL] 查会话阻塞关系,层次关系.
with ash as (select /*+ materialize*/* from DBA_HIST_ACTIVE_SESS_HISTORY where sample_time between ...
- 关于RuntimException
对于实现接口的类如果要抛出异常的话,那么接口也要抛出异常 所以RuntimeException只要对于实现接口的类就可以了 对于继承的类也可以这样运用 毕竟在实际开发中接口不一定是自己写的,而且团队可 ...
- 旋转图css3
<!doctype html><html> <head> <meta charset="UTF-8"> <title> ...
- OpenGL中shader读取实现
1.需要shader在OpenGL中工作,必须经过如下过程 2.代码实现 /********** * loadshader.h **********/ #pragma once #define _CR ...
- MyBaits 错误分析
错误原因:在DAO的映射文件中,在映射标签中的type类型写成DAO类了,应该写成javaBean