• 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]; frame seg.frame=CGRectMake(20,100,200,40); 代理方法 -(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 代理方法 -(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{         以imgView宽和高中较大的为标准进行等比缩放,如果imgView的尺寸不够,则负方向进行调整,但不会影响imgView的frame imgView.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; 左边加view field.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];   } 是否激活,默认YES label.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:@" "]) {     删除段 [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(@"键盘消失");                                                
                      }                                                
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
                                                                       
  • 相关阅读:
    linux修改host文件
    SpringBoot RocketMQ 整合使用和监控
    Linux的五个查找命令
    github 在线创建文件和创建文件夹
    nginx 配置
    JetBrains 里不为人知的秘密(3)--快捷键篇
    java.lang.NoClassDefFoundError: Lorg/springframework/beans/factory/access/BeanFactoryReference;
    【数论】P1029 最大公约数和最小公倍数问题
    ST表学习笔记
    树状数组学习笔记
  • 原文地址:https://www.cnblogs.com/zyz1341320997/p/5012219.html
Copyright © 2020-2023  润新知