01【iOS总结】UIView、UILabel、UITextField、UIButton 、目标动作机制(+UIAlertView、UIAlertController)
来源:互联网 发布:手机淘宝还用装旺信吗 编辑:程序博客网 时间:2024/04/28 19:51
01.UIView、UILabel、UITextField、UIButton 、目标动作机制(+UIAlertView、UIAlertController)
1.UIView
他是整个UI控件的父类,大部分控件的初始化方法都使用
// 1.创建一个UIview对象(初始化)并设置它的frame(一个矩形) UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 150)]; // 2.设置它的背景颜色 myView.backgroundColor = [UIColor redColor]; // 改变myView的bounds [myView setBounds:CGRectMake(50, 50, 100, 150)]; // 改变myView的center [myView setCenter:CGPointMake(100, 100)]; // 3.呈现view对象 [self.window addSubview:myView]; // 得到myView父view id supView = myView.superview; // 隐藏当前view myView.hidden = NO; // 设置透明度,取值范围(0,1),0纯透明,1不透明 myView.alpha = 0.5; // 得到他的所有子类view 如果需要得到子视图中的某一类视图,需要遍历的时候配合iskindofclass来使用// myView.subviews;for(UIView *subview in [cell.contentView subviews])//获取view的子视图 //UITabViewCell中的contentView包括textlabel、imageView[subview removeFromSuperview];//从父视图中移除子视图 [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:4];// 交换两个指定索引位置的子视图 [self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:3]; [self.window bringSubviewToFront: view5]; // 经某个view将该对象呈现在最外层
2.UIlable
需要掌握初始化方法,基本属性的使用。可以添加到父视图上。它主要侧重内容的显示。
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, realSize.width, realSize.height)];UILabel* label=[[UILabel alloc] initWithFrame:CGRectZero];//不指定大小的初始化label.numberOfLines=0;//设置最大行数,超过指定行则截断用省略号表示,默认为1,为0表示不限制label.numberOfLines=3//限制行数有三行label.lineBreakMode=UILineBreakModeCharacterWrap;//设置折行方式label.text=str;label.textColor=[UIColor blueColor];lable.textAlignment=UITextAlignmentCenter;//设置字体对齐方式label.backgroundColor=[UIColor clearColor]; //设置label背景色label.font=font;label.frame=CGRectMake(0,0,1,1);self.contentLabel=label;int val=[label.text intValue];//NSString到 intValue的转换UILabel* label=(UILabel*)[self.view viewWithTag:100];//每一个viewController都有一个self.view,viewWithTag:100找出该view上tag值为100的控件,返回值为UIView*类型[self.view addSubview:self.contentLabel];[label addSubview:img];
3.UITextField
需掌握初始化方法,基本属性的使用。要多熟悉它的代理方法。它主要侧重于用户的输入。
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 70)]; textField.backgroundColor = [UIColor greenColor];// textField.text = @"请输入用户名:"; // 给字体设置颜色 textField.textColor = [UIColor blueColor]; // 改变字体大小 textField.font = [UIFont systemFontOfSize:12]; // 得到系统所支持的所有字体 NSLog(@"%@",[UIFont familyNames]);// 打印系统提供的字体 // 设置字体 textField.font = [UIFont fontWithName:@"Apple SD Gothic Neo" size:50];// // 设置占位字符(提示性文字) textField.placeholder = @"请输入邮箱:"; // 设置键盘样式// textField.keyboardType = UIKeyboardTypeNamePhonePad; // 设置键盘外观 textField.keyboardAppearance = UIKeyboardAppearanceDark; // 自动纠错 textField.autocorrectionType = UITextAutocorrectionTypeYes; // 设置首字母大写(当首字母不能大写的时候,有可能和键盘样式有关) textField.autocapitalizationType = UITextAutocapitalizationTypeWords; // 一键清除(右边的小叉号) textField.clearButtonMode = UITextFieldViewModeAlways; // 当变成编辑状态时,让该textField刚才所有的输入清除 textField.clearsOnBeginEditing = YES; // 打开密码格式(密码样式可自定义) textField.secureTextEntry = YES; // 设置背景图片(如果不是png格式的,必须加后缀名,因为当没有后缀名的时候,系统默认是PNG格式的) textField.background = [UIImage imageNamed:@"picture.jpg"]; // 设置边框样式(圆角扁平状) textField.borderStyle = UITextBorderStyleLine; // 文字的居左、居中、居右 textField.textAlignment = NSTextAlignmentCenter; // 设置文字的垂直位置(Label没有该属性) textField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter; // 设置文字的水平位置(Label没有该属性) textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // 设置文字的水平位置(Label没有该属性) textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; // 不让进行编辑(交互) // enabled:直接改变控件本身的状态,例如:textField有可编辑状态和不可编辑状态。当设置为NO时,textField就会从可编辑状态改变到不可编辑状态。 // userInteractionEnabled:不改变控件的状态,但是把用户交互关闭了,就是用户不可对该控件进行操作。 textField.enabled = NO;// textField.userInteractionEnabled = NO; //自定义添加辅助视图(自定义) UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)]; UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 50)]; [accessoryView setBackgroundColor: [UIColor yellowColor]]; textField.inputAccessoryView = accessoryView; //自定义输入视图(默认是键盘) UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)]; inputView.backgroundColor = [UIColor grayColor]; textField.inputView = inputView; //左视图 UILabel *leftLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 100)]; leftLable.backgroundColor = [UIColor purpleColor]; textField.leftView = leftLable; textField.leftViewMode = UITextFieldViewModeAlways; //右视图 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"picture.jpg"]]; textField.rightView = imageView; textField.rightViewMode = UITextFieldViewModeAlways; [self.window addSubview:textField];
4.UIButton
按钮的初始化方法是使用的自己的,是一个便利构造器。这个和前两个有很大区别。要注意。按钮还有点击事件。很多时候点击无反应是我们将它的touchevent设置有误,最常用的是单击事件。
//UIButton按钮自有的初始化方法 UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];//便利构造器 /* UIButtonTypeCustom = 0, UIButtonTypeDetailDisclosure, UIButtonTypeInfoLight, UIButtonTypeInfoDark, UIButtonTypeContactAdd, UIButtonTypeRoundedRect = UIButtonTypeSystem, */ [btn setFrame:CGRectMake(200, 400, 100, 50)]; //设置按钮标题 // normal:是按钮常态,不做任何操作和设置情况下看到的按钮状态 // highlighted:用户按住按钮不松手时候看到的状态 // Disabled:关闭交互的状态的标题 // Selected:按钮被选中状态 // UIControlStateApplication:按钮作为应用启动图标 // UIControlStateReserved:预留字段 [btn setTitle:@"千万别摸我" forState:UIControlStateNormal]; [btn setTitle:@"摸了就摸了" forState:UIControlStateHighlighted]; // 关闭交互的状态的标题// [btn setTitle:@"摸我也没用" forState:UIControlStateDisabled];// [btn setEnabled:NO]; // 按钮被选中状态 [btn setTitle:@"一直在被摸" forState:UIControlStateSelected]; // 设置按钮被选中// [btn setSelected:YES]; // 改变标题颜色 btn.tintColor = [UIColor orangeColor]; [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted]; // 当点击时候有光圈效果 btn.backgroundColor = [UIColor blueColor]; btn.showsTouchWhenHighlighted = YES;
5.目标动作机制
{ //给按钮添加点击事件(目标动作机制!) // Target:方法选择器的执行者(回调者) // action:按钮的回调方法(点击所触发的事件)如果回调方法带参数,只能是按钮本身 // events:触发事件的点击模式// [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];// btnAction是个方法名,需要实现该方法 // 不带参 [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];// btnAction是个方法名,需要实现该方法, //关闭多点触控 btn.multipleTouchEnabled = NO; [self.window addSubview:btn]; textField.tag = 1000; return YES;}- (void)btnAction: (id)sender{ UIButton *btn = (UIButton *)sender; [btn setTitle:@"你是被莫大的" forState:UIControlStateNormal]; // 随机更改window颜色 float a = arc4random()%256; self.window.backgroundColor = [UIColor colorWithRed:a/255.0 green:a/255.0 blue:a/255.0 alpha:1.0]; //点击按钮可以得到文本输入框中的文字 //textField不能是全局变量或者属性 // 给field添加tag值,tag值的作用是,为当前view或者其子类添加标记,当前view或者其子类的父视图可以通过tag找到该视图。 // 父视图通过tag值得到field UITextField *resultTextField = (UITextField *)[self.window viewWithTag:1000]; // 得到文本输入框中的文字 NSString *fieldTextStr = resultTextField.text; NSLog(@"%@", fieldTextStr); NSLog(@"你点");}
6.UIAlertView
//添加警示框- (void)viewDidLoad { [super viewDidLoad]; //1.创建button UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; //设置按钮的frame和title [btn setFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@"警示框" forState:UIControlStateNormal]; //设置点击事件 [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; //显示按钮 [self.view addSubview:btn];}//4.实现btn点击事件- (void)btnAction: (UIButton *)sender{ // 创建一个UIAlertView对话框 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"我是提示标题" message:@"我是内容" delegate:self cancelButtonTitle:@"我是取消" otherButtonTitles:@"我是确定1", @"我是确定2", nil]; //显示alterView [alertView show];}
7.UIAlertController
//添加警示框- (void)viewDidLoad { [super viewDidLoad]; //新型弹出框按钮 UIButton *newBtn = [UIButton buttonWithType:UIButtonTypeSystem]; [newBtn setTitle:@"新警示框" forState:UIControlStateNormal]; [newBtn setFrame:CGRectMake(300, 200, 100, 100)]; [newBtn addTarget:self action:@selector(newBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:newBtn];}//新弹出框按钮事件- (void)newBtnAction: (UIButton *)sender{ // 创建一个警示框控制器 // 这是iOS8之后出的新类,用来替换原来的UIAlertView 和 UIActionSheet (不需要代理) UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"新标题" message:@"发红包" preferredStyle: UIAlertControllerStyleAlert]; //为警示框添加点击按钮(内部实现) //title:警示框按钮标题 //style:按钮的样式 //hander:点击按钮就会执行的语句(block) UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { NSLog(@"我点击了取消按钮"); }]; UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ NSLog(@"我点击了确定按钮"); }]; //将创建好的action添加到警示框视图控制器上 [alertC addAction:cancelAction]; [alertC addAction:sureAction]; //视图控制器之间切换的一种样式,称之为【模态】 //Controller:将要模态出来的视图控制器 //animated:模态推出视图控制器的过程是否需要动画 //completion:模态推出视图控制器之后将要执行的操作 [self presentViewController:alertC animated:YES completion:^{ NSLog(@"我是新警示框的视图控制器,我将要显示...."); }];}
注意:
1.bounds 是针对自己的坐标系系统
改变自身 bounds 的值不会影响到自身的 x,y ,只会影响到子视图的坐标系!
如果 bounds 的值改为负数,它自身的原点会向右下偏移
如果 bounds 的值改为正数,它自身的原点会向左上偏移
0 0
- 01【iOS总结】UIView、UILabel、UITextField、UIButton 、目标动作机制(+UIAlertView、UIAlertController)
- UIView UILabel UITextField UIButton
- UIView,UILabel,UITextField,UIButton使用方法
- UI_基础视图(UILabel UITextField UIButton UIAlertView)
- UI基本控件-UILabel-UITextField-UIButton-UIAlertView
- **原创**零基础学ios**UI课程01***课堂笔记整理*UIView UILabel UIButton UITextField**
- UIView UILabel UITextFiled UIButton UIControlView UIAlertView
- UIView UILabel UITextField UIButton UIImageView 常用属性
- UILabel 、UIbutton 、 UITextField 代码总结
- iOS基本常用控件总结(UIView,UILabel,UIButton,UIImageView)
- UIButton与目标动作机制
- IOS UI02UILabel,UITextField,UIButton,UIAlertView,delegate
- 基础控件之UILabel、UIButton、UITextField、UIAlertView、 UIImageView
- iOS学习笔记 4 —— 代码创建界面UIView、UILabel、UITextField、UIButton
- iOS开发:常用的UIView控件——UILabel、UITextField、UIButton
- iOS个人整理06-第一周小结,常用控件UIView,UILabel,UIImageView,UIButton,UITextField
- iOS开发:常用的UIView控件——UILabel、UITextField、UIButton【转】
- iOS基础——UI控件之UIView、UITextField、UILabel、UIColor、UIButton、UIImageView
- C 语言中的左值和右值。以及对比数组名和指针取数组元素的区别
- 深入理解Android之Gradle
- mybatis之map.xml文件的解读
- MongoDB is web scale
- SPI协议及工作原理分析
- 01【iOS总结】UIView、UILabel、UITextField、UIButton 、目标动作机制(+UIAlertView、UIAlertController)
- 并发编程网-线程池-说明
- 12.缺陷跟踪系统Mantis的问题生命周期和工作流
- 短消息调试笔记
- 正则表达式
- hibernate的native sql查询
- WAF 与 RASP 的安装使用大比拼!
- 用JAVA代码获取Weblogic配置的JNDI 数据源连接
- 二叉树二叉树虐我千万遍劳资今天终于搞明白了哈哈哈哈哈