UI常用控件总结(二)

UIPageControl

   //设置pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    //设置点的颜色
    pageControl.pageIndicatorTintColor = [UIColor blackColor];
    //设置当前的页面的点颜色
    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    //居中
    pageControl.center = self.view.center;
    //设置页面个数(必须设置,否则将不会显示)
    pageControl.numberOfPages = 8;
    //设置默认位置(从0开始)
    pageControl.currentPage = 1;
    //添加
    [self.view addSubview:pageControl];

UILabel

    //设置label
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    //设置label的背景颜色
    label.backgroundColor = [UIColor lightGrayColor];
    //居中
    label.center = self.view.center;
    //设置内容
    label.text = @"hello";
    //设置字体颜色
    label.textColor = [UIColor blackColor];
    //设置字体大小
    label.font = [UIFont systemFontOfSize:12];
    //设置内容对齐方式
    label.textAlignment = NSTextAlignmentCenter;
    //设置内容的阴影
    label.shadowColor = [UIColor blackColor];
    //设置阴影的大小
    label.shadowOffset = CGSizeMake(0, 1);
    //添加
    [self.view addSubview:label];

UITextField

  //设置TextField
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
    _textField = textField;
    //居中
    textField.center = self.view.center;
    //设置文本框类型
    textField.borderStyle = UITextBorderStyleRoundedRect;
    //设置文本框颜色
    textField.backgroundColor = [UIColor lightGrayColor];
    //设置输入提示
    textField.placeholder = @"请输入账号";
    //设置初始内容
//    textField.text = @"hello";
    //设置字体颜色
    textField.textColor = [UIColor blueColor];
    //设置字体大小
//    textField.font = [UIFont systemFontOfSize:26];
    //宽度自适应
    textField.adjustsFontSizeToFitWidth = YES;
    //在开始编辑的时候将原来的内容清空
    textField.clearsOnBeginEditing = YES;
    //清空的小叉号
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    //当左侧需要添加对象时,采用此方法显示(右侧同样适用)
    textField.leftViewMode = UITextFieldViewModeAlways;
    //内容设置成密码格式
//    textField.secureTextEntry = YES;
    //设置首字母自动大写(按句子划分,以回车为标志,同样也可以单词等划分)
    textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;
    //单词自动纠正
    textField.autocorrectionType = UITextAutocorrectionTypeDefault;
    //设置回车键类型
    textField.returnKeyType = UIReturnKeyJoin;
    //设置键盘类型
    textField.keyboardType = UIKeyboardTypePhonePad;
    //设置键盘的颜色(黑/白)
    textField.keyboardAppearance = UIKeyboardAppearanceDark;
    //添加
    [self.view addSubview:textField];
    //设置键盘上方工具条(只需设置工具条尺寸即可)
    UIView *inputAccess = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    inputAccess.backgroundColor = [UIColor greenColor];
    textField.inputAccessoryView = inputAccess;
    //自定义键盘
//    UIView *inputView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 200)];
//    inputView.backgroundColor = [UIColor orangeColor];
//    textField.inputView = inputView;

    //设置label来显示文本框输入的内容
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake((kWidth-200)/2.0, 20, 200, 50)];
    _textLabel = textLabel;
    textLabel.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:textLabel];

    //系统自带的代理方法
    textField.delegate = self;
  • 注意:以下方法均是系统自带的代理方法,需要设置:textField.delegate = self;
  • pragma mark 关闭键盘及显示输入内容
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //放弃第一响应者(关闭键盘)
    [_textField resignFirstResponder];
    //显示框中显示文本框输入的内容(在此处调用,将不会同步)
    _textLabel.text = _textField.text;

}
  • pragma mark 文本框将要被编辑的时候调用
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    NSLog(@"%s",__func__);
    return YES;
}
  • pragma mark 文本框开始被编辑的时候调用
-(void)textFieldDidBeginEditing:(UITextField *)textField{

    NSLog(@"%s",__func__);

}
  • pragma mark 文本框将要结束编辑的时候
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"%s",__func__);

    return self;
}
  • pragma mark 文本框结束编辑的时候
-(void)textFieldDidEndEditing:(UITextField *)textField{

    NSLog(@"%s",__func__);
}

UIImageView

  //设置imageView
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //设置图片
    imageView.image = [UIImage imageNamed:@"1"];
    //开启用户交互(当按钮加在imageView上是,imageView需要开始用户交互)
    imageView.userInteractionEnabled = YES;
    //添加
    [self.view addSubview:imageView];

    //设置一个数组用来存放动画图片(假设该数组中已经存放好图片)
    NSMutableArray *animateArray = [NSMutableArray new];
    //设置要动画的图片
    imageView.animationImages = animateArray;
    //设置动画次数
    imageView.animationRepeatCount = 10;
    //设置动画时长
    imageView.animationDuration = 5;
    [imageView setAnimationDuration:5];
    //动画开始
    [imageView startAnimating];
    //动画结束
    [imageView stopAnimating];

UISegmentedControl

   NSArray *array = @[@"黄色",@"红色",@"绿色"];
    //设置UISegmentedControl
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:array];
    _segmentControl = segmentControl;
    //设置尺寸
    segmentControl.frame = CGRectMake(0, 0, 200, 30);
    //居中
    segmentControl.center = self.view.center;
    //设置记录选中状态
    segmentControl.momentary = NO;
    //获取当前分块个数
    NSInteger num = segmentControl.numberOfSegments;
    NSLog(@"%ld",num);
    //设置背景色
    segmentControl.backgroundColor = [UIColor grayColor];
    //设置默认选中的按钮(从0开始)
    segmentControl.selectedSegmentIndex = 2;
    //通过属性字典,设置标题的颜色跟字体的大小
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14],NSUnderlineStyleAttributeName:@1};
    [segmentControl setTitleTextAttributes:dic forState:UIControlStateNormal];
    //监视点击事件(这里选择UIControlEventValueChanged事件,通过selectedSegmentIndex来判断选中的是哪个)
    [segmentControl addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];
    //添加
    [self.view addSubview:segmentControl];
  • pragma mark 点击事件change
-(void) change:(UISegmentedControl *) seg{

    //获取当前选中的索引
    NSInteger index=seg.selectedSegmentIndex;

    switch (index) {
        case 0:
            self.view.backgroundColor=[UIColor redColor];
            break;
        case 1:
            self.view.backgroundColor=[UIColor greenColor];
            break;
        case 2:
            self.view.backgroundColor=[UIColor yellowColor];
            break;

        default:
            break;
    }
}
  • pragma mark 点击空白处,插入,删除item
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //插入一个item
    [_segmentControl insertSegmentWithTitle:@"白色" atIndex:2 animated:YES];
    //取消一个item
    [_segmentControl removeSegmentAtIndex:1 animated:YES];
}

UIProgressView

  //设置进度条
    UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    //居中
    progressView.center = self.view.center;
    //设置当前进度(默认范围0~1)
    progressView.progress = 0.5;
    //设置已经覆盖的进度颜色
    progressView.progressTintColor = [UIColor blueColor];
    //设置已经覆盖的进度图片
    progressView.progressImage = [UIImage imageNamed:@"1"];
    //设置轨道的颜色
    progressView.trackTintColor = [UIColor grayColor];
    //设置轨道图片
    progressView.trackImage = [UIImage imageNamed:@"2"];

    //添加
    [self.view addSubview:progressView];

UIScrollView

   //设置UIScrollView
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    _scrollView = scrollView;
    //设置代理
    scrollView.delegate = self;
    //设置内容的大小
    scrollView.contentSize = CGSizeMake(kWidth+100, kHeight+100);
    //去掉弹簧效果
    scrollView.bounces = NO;
    //去掉滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    //开启分页效果
    scrollView.pagingEnabled = YES;
    //控制当前视图的位置
    scrollView.contentOffset = CGPointMake(kWidth, 0);
    //添加
    [self.view addSubview:scrollView];
  • 注意:调用以下方法必须设置代理
  • pragma mark 滚动视图在滚动过程中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

}
  • pragma mark 滚动视图在没次滚动结束后
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{


}
原文地址:https://www.cnblogs.com/my-garden/p/5569465.html