iOS--基础控件总结一

UIWindow窗口UIView视图UIButten按钮UILabel文本显示UITextField输入框UI TextView多行输入框UISwitch开关UISegmentedControl分段控制UIAlertView提示UIActionSheet提示UIImage图片视图UIImage图片视图UIImageView图片视图UITapGestureRecognnizer手势UIScrollView滚动视图UIPageControl页面视图UITableView列表视图
 定义 self.window = [UIWindow alloc] initWithFrame:self.view.bounds];定义UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];定义UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];定义UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 100, 300)];定义field = [[UITextField alloc]initWithFrame:CGRectMake(20, 40, 200, 30)];定义UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 20, 200, 30)];定义UISwitch*switch1 = [[UISwitchalloc]initWithFrame:CGRectMake(30,50,0,0)];定义UISegmentedControl*seg = [[UISegmentedControlalloc]initWithItems:@[@"122",@"123",@"121"]];创建UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"提示内容"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"按钮1",@"按钮2",nil];创建UIActionSheet*action = [[UIActionSheetalloc]initWithTitle:nildelegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"建行",@"农行",nil];定义UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"5.jpg"]];创建UIImage*im1 = [UIImageimageWithContentsOfFile:@"/Users/shangchengjiaoyu/Desktop/学习资料/图片/9c9b4d37ee9cd67ceb10bb2a7b3dd2e2.jpg"];创建imgView= [[UIImageViewalloc]initWithFrame:self.view.bounds];创建UITapGestureRecognizer*tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap1:)];创建UIScrollView*scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(10,20,WIDTH-20,HEIGHT-30)];创建page= [[UIPageControlalloc]initWithFrame:CGRectMake(100,600,200,30)];创建UITableView*tableView = [[UITableViewalloc]initWithFrame:self.view.boundsstyle:UITableViewStylePlain];
背景颜色window.backgroundColor = [UIColor whiteColor];背景颜色[view.backgroundColor = [UIColor grayColor];背景颜色[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];背景颜色label.backgroundColor = [UIColor colorWithRed:26/255.f green:124/255.0 blue:248/255.0 alpha:1];背景颜色field.backgroundColor = [UIColor yellowColor];背景颜色textView.backgroundColor = [UIColor grayColor];圆的颜色switch1.thumbTintColor= [UIColoryellowColor];边框和选中颜色seg.tintColor= [UIColorredColor];设置内容alert.message=@"11111";  范围imageView.frame = CGRectMake(10, 20, 100, 200);获取宽高floatw = im1.size.width;//图片宽,和frame无关系          float H = im2.size.height;//高调整imageView,调整其尺寸适应图片大小,(imgView的size等于图片的size[imgView sizeToFit];点击次数tap.numberOfTapsRequired=2;设置背景颜色scroll.backgroundColor= [UIColorwhiteColor];设置背景颜色page.backgroundColor= [UIColoryellowColor];分割线颜色tableView.separatorColor= [UIColorredColor];
让其成为主窗口切可视[self.window makeKeyAndVisible];加入父视图[self.view addSubview:view];加入父视图[self.view addSubview:button];加入父视图[self.view addSubview:label];加入父视图[self.view addSubview:field1];加入父视图[self.view addSubview:textView];加入父视图[self.view addSubview:switch1];加入父视图[self.view addSubview:seg];添加[alertshow];添加[actionshowInView:self.view];加入父视图[self.view addSubview:imageView];添加UIImageView *imageView = [[UIImageView alloc] initWithImage:im4];//
    imageView.frame = CGRectMake(20, 30, 100, 200);
    [self.view addSubview:imageView];
输出其imgView的尺寸NSLog(@"%@",NSStringFromCGRect(imgView.frame));同一个手势不能添加到多个视图上[imageViewaddGestureRecognizer:tap];添加到父视图[self.viewaddSubview:scroll];添加到主视图[self.viewaddSubview:page];添加到父视图[self.viewaddSubview:tableView];
设置跟视图控制器self.window.rootViewController = [[ViewController alloc] init];设置标签view.tag = 1;设置标签butten.tag = 1;设置标签label.tag = 1;设置标签field.tag = 1;设置标签textView。Tag = 1;设置标签switch1.tag=1;设置标签seg.tag = 1;设置标题alert.title=@"123";设置标签action.tag=4;  图片转换成Data,第一个是要转换的图片对象,第二个参数代表压缩质量(0~1浮点),1代表不压缩NSData*data1 =UIImageJPEGRepresentation(im3,1);以imgView宽和高中较小的为标准进行比例缩放imgView.contentMode = UIViewContentModeScaleAspectFit;手指个数、单击tap.numberOfTouchesRequired = 2;设置标签scroll.tag = 1;设置Page的页数page.numberOfPages=4;分割线样式tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
*流程*main ->app ->view ->…获取父视图中标签为一的视图UIView *view1 = [self.view viewWithTag:1];获取按钮的选中状态[button setTitle:@"按钮" forState:UIControlStateSelected]; button.selected = YES;   NSLog(@"%d",button.isSelected);文本label.text = @"shang udh w hd ewihidwijdiwjediwje";边框类型field.borderStyle =UITextBorderStyleBezel;边框类型textView.layer.cornerRadius=10;//设置圆角,如果想要原型,需要设置为高的一半边框颜色switch1.tintColor= [UIColorgreenColor];frameseg.frame=CGRectMake(20,100,200,40);代理方法-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{代理方法-(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    以imgView宽和高中较大的为标准进行等比缩放,如果imgView的尺寸不够,则负方向进行调整,但不会影响imgView的frameimgView.contentMode = UIViewContentModeScaleAspectFill;拖动手势UIPanGestureRecognizer*pan = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan:)];//拖动手势                        [imageView1 addGestureRecognizer:pan];设置滚动范围scroll.contentSize=CGSizeMake((WIDTH+20)*2, (HEIGHT+30)*2);设置选中叶page.currentPage=0;行高tableView.rowHeight=100;
  是否允许用户交互view.userInteractionEnabled = NO;设置字体大小button.titleLabel.font = [UIFont systemFontOfSize:40];文本颜色label.textColor = [UIColor whiteColor];提示内容field.placeholder = @"888";是否允许编辑textView.editable = NO;开后颜色switch1.onTintColor= [UIColorblueColor];给某一段设置标题[seginsertSegmentWithImage:[UIImageimageNamed:@"avatar"]atIndex:3animated:YES];     //根据被点击按钮的索引处理点击事件,buttonIndex表示被点击的按钮的下标,默认cancel0    NSLog(@"=====%zi",buttonIndex);    图片显示img的原始大小,显示位置位于imgView的中心,如果imgView的尺寸不够,则负方向调整imgView.contentMode = UIViewContentModeCenter;点击事件-(void)tap1:(UITapGestureRecognizer*)img{是否允许滚动scroll.scrollEnabled=YES;未选中页的颜色page.pageIndicatorTintColor= [UIColorredColor];添加视图tableView.backgroundView = imageView;
  子视图影响父视图self.view.userInteractionEnabled = NO;设置背景图,图片自动充满[button setBackgroundImage:[UIImage imageNamed:@"12"] forState:UIControlStateNormal];字体大小label.font = [UIFont systemFontOfSize:18];再次编辑时消失field.clearsOnBeginEditing = YES;是否允许滚动textView.scrollEnabled = NO;开关状态switch1.On=YES;设置标题[seg setTitle:@"分段" forSegmentAtIndex:3]; }    NSLog(@"---->%@",[actionSheetbuttonTitleAtIndex:buttonIndex]);    图片数组NSMutableArray*imgarr = [[NSMutableArrayalloc]init];     NSLog(@"dianji");设置弹簧效果scroll.bounces=YES;选中页的颜色page.currentPageIndicatorTintColor= [UIColorblackColor];TableView的底部视图UIView*footer = [[UIViewalloc]initWithFrame:CGRectMake(10,600,100,100)];
  设置透明度self.view.alpha = 0.1;点击事件[button addTarget:self action:@selector(settitle) forControlEvents:UIControlEventTouchUpInside];系统字体加粗label.font = [UIFont boldSystemFontOfSize:18];背景图field.background = [UIImage imageNamed:@""];弹簧效果textView.bounces = NO;点击事件[switch1addTarget:selfaction:@selector(myswitch:)forControlEvents:UIControlEventValueChanged];获取标题NSString*s = [segtitleForSegmentAtIndex:1];              for(inti =1; i<6; i++) {     UIView *v = img.view;设置滚动到哪个位置scroll.contentOffset=CGPointMake(50,50);选中事件[pageaddTarget:selfaction:@selector(p:)forControlEvents:UIControlEventValueChanged];    footer.backgroundColor= [UIColorgrayColor];
  设置视图是否隐藏view.hidden = YES;设置背景图[button setImage:[UIImage imageNamed:@"12"] forState:UIControlStateNormal];自定义字体,前面代表字体名后面代表大小label.font = [UIFont fontWithName:<#(NSString *)#> size:<#(CGFloat)#>];清除按钮设置field.clearButtonMode = UITextFieldViewModeAlways;边框颜色textView.layer.borderColor= [UIColorredColor].CGColor;点击方法-(void)myswitch:(UISwitch *)swi{获取图片UIImage*image = [segimageForSegmentAtIndex:3];      if(actionSheet.tag==3) {            UIImage*image = [UIImageimageNamed:[NSStringstringWithFormat:@"%d.jpg",i]];//     NSLog(@"点击:%zi",v.tag);//点击事件获取当前滚动到位置NSLog(@"%@",NSStringFromCGPoint(scroll.contentOffset));      tableView.tableFooterView= footer;
  子视图影响父视图self.view.hidden = YES;设置图片的偏量button.imageEdgeInsets = UIEdgeInsetsMake(-30, 0, 0, 0);分两行输入label.numberOfLines = 2;左边加viewfield.leftView = view;边框颜色textView.layer.borderWidth=1;     NSLog(@"点击开关");设置某段的宽[segsetWidth:100forSegmentAtIndex:2];          UITextField*field = (UITextField*)[self.viewviewWithTag:1];             [imgarraddObject:image]; }允许整页滚动scroll.pagingEnabled=YES;  数据源代理tableView.dataSource=self;
  将子视图加到父视图的某个位置[view insertSubview:view1 atIndex:1];设置标题的偏量button.titleEdgeInsets = UIEdgeInsetsMake(100, -160, 0, 0);换行样式label.lineBreakMode = NSLineBreakByWordWrapping;弹出视图,类似于键盘field.inputView = view; textView.layer.masksToBounds=YES;     //UISwitch *s = (UISwitch *)[self.view viewWithTag:1];设置整体不被激活[seg setEnabled:NO];           field.text= [actionSheetbuttonTitleAtIndex:buttonIndex];         }拖动事件-(void)pan:(UIGestureRecognizer*)pan1{显示水平滚动条scroll.showsHorizontalScrollIndicator=YES;  代理tableView.delegate=self;
  view1放到view2之上[self.view insertSubview:view2 aboveSubview:view1];设置按钮是否被选中、获取选中状态button.selected = NO;NSLog(@"----%d",button.isSelected);对齐方式:右对齐label.textAlignment = NSTextAlignmentRight;左边加视图field.leftView = view;代理textView.delegate=self;     NSLog(@"---%d",swi.isOn);设置某段不被激活[segsetEnabled:NOforSegmentAtIndex:1];       }    图片数组   imgView.animationImages= imgarr;    //NSLog(@"%@",NSStringFromCGPoint([pan1 locationInView:self.view]));//输出point显示垂直滚动条scroll.showsVerticalScrollIndicator=YES;  消除多余分割线tableView.tableFooterView= [[UITableViewalloc]init];
  view1放到view2之下[self.view insertSubview:view2 belowSubview:view1];点击方法-(void)settitle{文本的阴影颜色label.shadowColor = [UIColor blackColor];设置左边加的视图field.leftViewMode = UITextFieldViewModeAlways;设置键盘返回键样式textView.returnKeyType=UIReturnKeyDone;  设置默认选中项seg.selectedSegmentIndex=1;   }    循环一次时间imgView.animationDuration=3;    NSLog(@"状态:%zi",pan1.state);//输出拖动开始、拖动、结束点状态栏,回到顶部scroll.scrollsToTop=YES;    
  交换位置[self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:2];     NSLog(@"按钮点击事件");文本的阴影偏移label.shadowOffset = CGSizeMake(2, 3);代理field.delegate = self;是否点击了return键-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{  获取当前被选中的段NSLog(@"选中:%zi",seg.selectedSegmentIndex);        循环次数,0为无限循环imgView.animationRepeatCount=0;    CGPointpoint = [pan1locationInView:self.view];//拖动缩放scroll.zoomScale=2;    
  将子视图移到父视图最前面[self.view bringSubviewToFront:view]; }是否激活,默认YESlabel.enabled = NO;设置键盘返回键样式field.returnKeyType = UIReturnKeyDone;    NSLog(@"====%@",text);   [segsetBackgroundImage:[UIImageimageNamed:@"btn_select"]forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];        开始循环[imgViewstartAnimating];     pan1.view.center= point;设置代理scrollView.delegate=self;    
  将子视图移到父视图最底层[self.view sendSubviewToBack:view];  设置新文本-(void)setLabelText{设置键盘为数字键field1.keyboardType = UIKeyboardTypeNumberPad;      绑定监听事件[seg addTarget:selfaction:@selector(segmentChange) forControlEvents:UIControlEventValueChanged];        停止动画[imgViewstopAnimating];  最大缩放范围scrollView.minimumZoomScale=1;    
  点击事件-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{        UILabel *label = (UILabel *)[self.view viewWithTag:1];是输入为密码样式field1.secureTextEntry = YES;    //监听是否点击了return按键  添加段insertSegmentWithTitle:@"分段1" atIndex:0 animated:YES];       [self.view addSubview:imgView];允许交换imgView.userInteractionEnabled=YES;  最小缩放范围scrollView.maximumZoomScale=3;    
       NSLog(@"view被点击");       label.text = @"新文本";选中输入框-(void)textFieldDidBeginEditing:(UITextField *)textField{    if([textisEqualToString:@"\n"]) {  删除段[seg1 removeSegmentAtIndex:1 animated:YES];      -(void)stopDongHua{  }        
   }   }     if (textField == field) {         [textViewresignFirstResponder];   -(void)mysegment:(UISegmentedControl*)se {          [imgViewstopAnimating];//停止动画           
       -(void)land{         NSLog(@"第一个");     }      NSLog(@" dianji");                    
           NSLog(@"点了登陆");     }//第一种方法    returnYES;     UISegmentedControl*seg3 = (UISegmentedControl*)[self.viewviewWithTag:2];         imgView.image= [UIImageimageNamed:@"1.jpg"];           
       }     NSLog(@"开始编辑%zi",textField.tag);//第二种方法 }     self.seg4=(UISegmentedControl*)[self.viewviewWithTag:2];         NSLog(@"停止动画");           
       -(void)cancel{ }输出输入的内容-(void)textViewDidChange:(UITextView*)textView{      NSLog(@" 11111__%zi",seg3.selectedSegmentIndex);      }           
           NSLog(@"点了取消");输出输入的内容-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{    NSLog(@"---->%@",textView.text);      NSLog(@"2222__%zi",se.selectedSegmentIndex);                  
       }     NSLog(@"输入的内容%@",string); }      NSLog(@"333__%zi",self.seg4.selectedSegmentIndex);                  
             return YES;        a=20;                  
         }        NSLog(@"%d",a);                  
        点清除按钮后触发-(BOOL)textFieldShouldClear:(UITextField *)textField{     }                  
             NSLog(@"完全删除后");                        
             return YES;                        
         }                        
        点return键后键盘消失-(BOOL)textFieldShouldReturn:(UITextField *)textField{                        
             NSLog(@"return时触发");                        
             [textField resignFirstResponder];//键盘消失                        
             return YES;                        
         }                        
        点击屏幕后键盘消失-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{                        
             [self.view endEditing:YES];                        
             NSLog(@"键盘消失");                        
         }                        
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  
                                  

转载于:https://www.cnblogs.com/zyz1341320997/p/5012219.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值