iOS UI控件总结(全)
1.UIButton
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(60, 30, 250, 35); [btn setBackgroundColor:[UIColor blackColor]]; btn.titleLabel.font = [UIFont systemFontOfSize: 14.0]; [btn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal]; btn.layer.cornerRadius = 5.0; [btn setTitle:@"黑色" forState:UIControlStateNormal]; [btn setTitle:@"是否确认" forState:UIControlStateHighlighted]; [btn addTarget:self action:@selector(zoomInAction:) forControlEvents:UIControlEventTouchUpInside];//添加事件 [_helpScrView addSubview:btn];
2.UILabel
//UILabel 文本显示 _label1 = [[UILabel alloc]initWithFrame:CGRectMake(60, 75, 100, 25)]; //_label1.frame = CGRectMake(60, 75, 100, 25); _label1.backgroundColor = [UIColor grayColor]; _label1.text = @"文字显示!"; _label1.font = [UIFont fontWithName:@"Arial" size:12]; _label1.textColor = [UIColor blackColor]; _label1.numberOfLines = 2;//文本最多行数,为0时没有最大行数限制 _label1.textAlignment = NSTextAlignmentCenter;//文字居中 [_helpScrView addSubview:_label1];
3.UISwtich(滑块控件)
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(45, 110, 300, 50)]; [_helpScrView addSubview:slider]; [slider setMaximumValue:100];//设置最大值10 [slider setMinimumValue:0];//设置最小值0 slider.value = 5;//当前位置是4 slider.minimumTrackTintColor = [UIColor orangeColor];//设置移动前线条的颜色 slider.maximumTrackTintColor = [UIColor blackColor];//设置移动后线条的颜色 slider.minimumValueImage = [UIImage imageNamed:@"love_1.png"];//设置滚动条最左边的图片 slider.maximumValueImage = [UIImage imageNamed:@"love_2.png"];//设置滚动条最右边的图片 //slider.thumbTintColor = [UIColor grayColor];//设置中间按钮移动按钮的颜色 //将按钮换成图片,正常状态下 [slider setThumbImage:[UIImage imageNamed:@"love_3.png"] forState:UIControlStateNormal]; [slider addTarget:self action:@selector(function:) forControlEvents:UIControlEventValueChanged]; -(void)function:(UISlider*)sender { long values = lroundf(sender.value); _label1.text = [NSString stringWithFormat:@"%ld",values]; //这样的效果 }
4.UISlider(开关控件)
//开关控件(UISwitch) UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(50, 170, 280, 100)]; [_helpScrView addSubview:switchView]; BOOL setting = switchView.isOn;//获得开关状态 [switchView setOn:setting animated:YES];//设置开关状态 //将上面的绿色替换成橘黄色 switchView.onTintColor = [UIColor orangeColor]; //设置按钮的颜色 switchView.thumbTintColor = [UIColor redColor]; //开关控件外部的颜色 switchView.tintColor = [UIColor purpleColor]; [switchView addTarget:self action:@selector(switchTag:) forControlEvents:UIControlEventValueChanged]; - (void)switchTag:(UISwitch *)switchTag { BOOL setting = switchTag.isOn; [switchTag setOn:setting animated:YES]; if (setting) { NSLog(@"on"); }else { NSLog(@"off"); } }
5.UITextField(输入框)
UITextField *accountField = [[UITextField alloc]initWithFrame:CGRectMake(85, 70, 250, 35)]; [accountField setBorderStyle:UITextBorderStyleRoundedRect]; //外框类型 [accountField setFont:[UIFont systemFontOfSize:16.0]]; [accountField setTextColor:[UIColor grayColor]]; accountField.placeholder = @"用户名"; //默认显示的字 accountField.secureTextEntry = NO; //是否以密码形式显示 accountField.returnKeyType = UIReturnKeyDone; //return键变成什么键 accountField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X accountField.keyboardType = UIKeyboardTypeDefault;//键盘显示类型 accountField.delegate = self; //[accountField becomeFirstResponder];//设置键盘第一响应者 [_view1_top addSubview:accountField];
//收回键盘 UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)]; //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。 tapGestureRecognizer.cancelsTouchesInView = NO; //将触摸事件添加到当前view [self.helpScrView addGestureRecognizer:tapGestureRecognizer];
//按下Done按钮的调用方法,我们让键盘消失 -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return YES; } //收回键盘 -(void)keyboardHide:(UITapGestureRecognizer*)tap{ [_FieldPass resignFirstResponder]; [_textView resignFirstResponder]; }
6.UITextView(文本框)
//UITextView 文本框,不可输入,根据内容改变框大小 _textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 20, UIScreenWidth-60, 50)]; _textView.editable = NO;//设置是否可以编辑 _textView.delegate = self; _textView.textColor = [UIColor colorWithRed:61/255.0 green:61/255.0 blue:61/255.0 alpha:1]; _textView.font = [UIFont boldSystemFontOfSize:16]; _textView.scrollEnabled = NO;//设置是否可以滚动 _textView.layer.borderColor = [[UIColor brownColor]CGColor]; _textView.layer.borderWidth = 1.0; _textView.layer.backgroundColor = [[UIColor clearColor] CGColor]; _textView.layer.cornerRadius = 6.0; _textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//相对于父视图不会改变属性 [_textView setSelectable:YES]; _textView.text = @"我多么希望喝酒的时候旁边有个人深情地看着我温柔的说,少喝点,伤身体,妈逼现实中却总是有个煞笔在旁边吼:你麻痹,养鱼啊!喝起!"; [_view1_top2 addSubview:_textView]; [self textViewDidChange:_textView]; //UITextView 文本框,不可输入 _textView2 = [[UITextView alloc]initWithFrame:CGRectMake(20, 180, UIScreenWidth-60, 140)]; _textView2.editable = NO;//设置是否可以编辑 _textView2.delegate = self; _textView2.textColor = [UIColor colorWithRed:61/255.0 green:61/255.0 blue:61/255.0 alpha:1]; _textView2.font = [UIFont boldSystemFontOfSize:13]; _textView2.scrollEnabled = NO;//设置是否可以滚动 _textView2.layer.borderColor = [[UIColor orangeColor]CGColor]; _textView2.layer.borderWidth = 1.0; _textView2.layer.backgroundColor = [[UIColor clearColor] CGColor]; _textView2.layer.cornerRadius = 6.0; _textView2.text = @"UITextView是继承于UIScrollView,并且遵循UITextInput的协议;而UIScrollView是继承于UIView,并且遵循NSCoding协议;上面有些属性跟UITextField是一样的,就没有标注出来;关于UITextInput的协议也可以见关于UITextField里面的内容;包含一些键盘响应及相应的通知处理知识点;from:http://www.cnblogs.com/wujy/p/5807646.html"; //可以检测检测电话、网址和邮箱。符合条件的文本中的内容就会高亮; _textView2.dataDetectorTypes = UIDataDetectorTypeAll; [_view1_top2 addSubview:_textView2]; //如何优雅的让UITextView根据输入文字实时改变高度 //UITextView 文本框,不可输入 _textView3 = [[UITextView alloc]initWithFrame:CGRectMake(20, 340, UIScreenWidth-60, 38)]; _textView3.editable = YES;//设置是否可以编辑 _textView3.delegate = self; _textView3.textColor = [UIColor colorWithRed:61/255.0 green:61/255.0 blue:61/255.0 alpha:1]; _textView3.font = [UIFont boldSystemFontOfSize:13]; _textView3.scrollEnabled = NO;//设置是否可以滚动 _textView3.layer.borderColor = [[UIColor orangeColor]CGColor]; _textView3.layer.backgroundColor = [[UIColor clearColor] CGColor]; _textView3.layer.borderWidth = 1.0; _textView3.layer.cornerRadius = 6.0; _textView3.layer.masksToBounds = YES; _textView3.textContainerInset = UIEdgeInsetsMake(10,0, 0, 0);//调整光标的位置,让光标出现在UITextView的正中间 [_view1_top2 addSubview:_textView3]; //uitextview 内置函数,代理方法 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ //UITextView: 响应键盘的 return 事件 if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return //在这里做你响应return键的代码 [textView resignFirstResponder]; return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行 } //每次输入文字后调用该方法,此时输入的文字并不在textView.text中,而在另一个参数text中,走完该方法后每次输入的文字才加入到textView.text中。解决方案是将textView.text和text文字拼接起来 CGRect frame = textView.frame; float height; if ([text isEqual:@""]) { if (![textView.text isEqualToString:@""]) { height = [ self heightForTextView:textView WithText:[textView.text substringToIndex:[textView.text length] - 1]]; }else{ height = [ self heightForTextView:textView WithText:textView.text]; } }else{ height = [self heightForTextView:textView WithText:[NSString stringWithFormat:@"%@%@",textView.text,text]]; } frame.size.height = height; [UIView animateWithDuration:0.5 animations:^{ textView.frame = frame; } completion:nil]; return YES; } //计算输入文字高度的方法 - (float) heightForTextView: (UITextView *)textView WithText: (NSString *) strText{ CGSize constraint = CGSizeMake(textView.contentSize.width , CGFLOAT_MAX); CGRect size = [strText boundingRectWithSize:constraint options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14]} context:nil]; float textHeight = size.size.height + 20.0; return textHeight; } //不可编辑的UITextView 自动调整高度 - (void)textViewDidChange:(UITextView *)textView{ CGRect frame = textView.frame; CGSize size = [textView.text sizeWithFont:textView.font constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:UILineBreakModeTailTruncation]; frame.size.height = size.height > 1 ? size.height + 20 : 64; textView.frame = frame; }
8.UISegmentedControl 选项卡控件
NSArray *arr = [NSArray arrayWithObjects:@"辰东", @"猫腻", @"天蚕土豆", @"开荒", nil]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:arr]; segmentedControl.frame = CGRectMake(20, 220, 320, 40); [_helpScrView addSubview:segmentedControl]; segmentedControl.tintColor = [UIColor orangeColor];//控件渲染色(也就是外观字体颜色) //设置字体样式 [segmentedControl setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13],NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal]; //添加一个分段(在指定下标下插入,并设置动画效果) [segmentedControl insertSegmentWithTitle:@"五金电料" atIndex:2 animated:NO]; //根据内容定分段宽度 segmentedControl.apportionsSegmentWidthsByContent = YES; //开始时默认选中下标(第一个下标默认是0) segmentedControl.selectedSegmentIndex = 0; //按下是否会自动释放: //segmentedControl.momentary = YES; //添加事件 [segmentedControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged]; //添加到导航栏上面 //self.navigationItem.titleView = segmentedControl; //点击不同分段就会有不同的事件进行相应 -(void)change:(UISegmentedControl *)sender{ if (sender.selectedSegmentIndex == 0) { NSLog(@"辰东"); }else if (sender.selectedSegmentIndex == 1){ NSLog(@"猫腻"); }else if (sender.selectedSegmentIndex == 2){ NSLog(@"五金电料"); }else if (sender.selectedSegmentIndex == 3){ NSLog(@"天蚕土豆"); }else{ NSLog(@"开荒"); } }
9.UIPageControl 分页,与UIScrollerView联合使用
_pageCtrl = [[UIPageControl alloc]initWithFrame:CGRectMake(20, UIScreenHeight-40, UIScreenWidth-40, 30)]; _pageCtrl.numberOfPages = 3;//设置控制器页数 _pageCtrl.currentPage = 0;//设置当前所在页码 _pageCtrl.pageIndicatorTintColor = [UIColor redColor];//设置控制器页码点得颜色 _pageCtrl.currentPageIndicatorTintColor = [UIColor blackColor];//设置控制器当前所在页码点的颜色 [_pageCtrl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:_pageCtrl]; //先加载uiscrollView _helpScrView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, bounds.size.width, UIScreenHeight)]; [_helpScrView setContentSize:CGSizeMake(UIScreenWidth*3, UIScreenHeight-80)];//设置页数 _helpScrView.pagingEnabled = YES; //设为YES时,会按页滑动 _helpScrView.contentOffset = CGPointMake(0, 0); _helpScrView.bounces = YES;//弹性,默认为yes [_helpScrView setDelegate:self];//协议 _helpScrView.showsHorizontalScrollIndicator = NO;//取消UIScrollView自己的进度条。 [self.view addSubview:_helpScrView]; //将UIScrollView添加到主界面上 //其次是UIScrollViewDelegate的scrollViewDidEndDecelerating函数,用户滑动页面停下后调用该函数。 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { //更新UIPageControl的当前页 CGPoint offset = scrollView.contentOffset; CGRect bounds = scrollView.frame; [_pageCtrl setCurrentPage:offset.x / bounds.size.width]; long numbers = lroundf(offset.x / bounds.size.width); NSLog(@"%ld",numbers);//显示页数 NSInteger juli = scrollView.contentSize.width-UIScreenWidth-1; NSLog(@"juli->>>>%ld",(long)juli); NSLog(@"scrollView.contentOffset.x->>>>%ld",(long)scrollView.contentOffset.x); // if (scrollView.contentOffset.x >juli) { // // NSLog(@"跳转!!"); // // Table_ViewController *secondview=[[Table_ViewController alloc]init]; // // [self.navigationController pushViewController:secondview animated:YES]; // } } //然后是点击UIPageControl时的响应函数pageTurn - (void)pageTurn:(UIPageControl*)sender { //令UIScrollView做出相应的滑动显示 CGSize viewSize = _helpScrView.frame.size; CGRect rect = CGRectMake(sender.currentPage * viewSize.width, 0, viewSize.width, viewSize.height); [_helpScrView scrollRectToVisible:rect animated:YES]; long numbers = lroundf(sender.currentPage); NSLog(@"%ld",numbers);//显示页数 }
10.UITableView
这个控件使用我只介绍一些关键的地方:
#pragma mark - 创建一个tableView -(void)createTableView{ _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WITH , SCREEN_HEIGHT - 49-64) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; } #pragma mark - tableView代理方法 //分组头部标题的高度 -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 40; } //分组头部区域插入View -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)]; view.backgroundColor = [UIColor colorWithRed:212/255.0 green:51/255.0 blue:36/255.0 alpha:1]; UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 100, 35)]; gameTimeModel *model = [[gameTimeModel alloc]init]; model = _dataSourceArray[section]; NSString *dataStr = model.date_block; timeLabel.text = dataStr; timeLabel.textColor = [UIColor whiteColor]; timeLabel.font = [UIFont systemFontOfSize:15]; [view addSubview:timeLabel]; return view; } //分组 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3;//分成三组 } //每组的cell数目 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 12;//每组12行 } //cell高度 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 90; } //这个就不用多说了,cell样式 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return nil ; }
注:此博客仅供博主学习总结用,一部分来自百度,如有侵权请联系我删除.