iOS常用UI控件

UISegmentedControl

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UISegmentedControl *segement = [[UISegmentedControl alloc] initWithItems:@[@”小学”,@”初中”,@”高中”]];
    segement.frame = CGRectMake(100, 200, 200, 40);
    [self.view addSubview:segement];

    //设置颜色
    segement.tintColor = [UIColor redColor];

    //设置标题
    [segement setTitle:@”大学” forSegmentAtIndex:2];

    //设置图片
    [segement setImage:[UIImage imageNamed:@”bj_top_jt@2x”] forSegmentAtIndex:0];

    //插入标题
    [segement insertSegmentWithTitle:@”高中” atIndex:2 animated:YES];

    //插入图片
    [segement insertSegmentWithImage:[UIImage imageNamed:@”Fav_Filter_ALL_HL@2x”] atIndex:4 animated:YES];

    //移除内容
    [segement removeSegmentAtIndex:4 animated:YES];
    // [segement removeAllSegments];

    //事件处理
    //除了button之外所有的点击控件都用UIControlEventValueChanged触发
    [segement addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];

}

  • (void)changeValue:(UISegmentedControl *)segment
    {
    NSLog(@”index == %ld”,segment.selectedSegmentIndex);
    NSLog(@”title == %@”,[segment titleForSegmentAtIndex:segment.selectedSegmentIndex]);
    // [segment imageForSegmentAtIndex:0];
    }

UISlider

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(100, 200, 200, 30)];

    //设置最小值
    slider.minimumValue = 100;

    //设置最大值
    slider.maximumValue = 1000;

    //设置滑过的颜色
    slider.minimumTrackTintColor = [UIColor redColor];

    //设置未滑过的颜色
    slider.maximumTrackTintColor = [UIColor greenColor];

    //设置滑块按钮的颜色
    slider.thumbTintColor = [UIColor blackColor];

    //添加事件
    [slider addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:slider];
    }

  • (void)changeValue:(UISlider *)slider
    {
    //当前value
    NSLog(@”slider == %g”,slider.value);
    }

UISwitch

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UISwitch *swth = [[UISwitch alloc] initWithFrame:CGRectMake(100, 200, 100, 30)];
    //设置开的颜色
    // swth.onTintColor = [UIColor redColor];

    //设置滑块的颜色
    // swth.thumbTintColor = [UIColor blackColor];

    swth.on = YES;

    //添加事件
    [swth addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:swth];
    }

  • (void)valueChange:(UISwitch *)swth
    {
    if (swth.isOn) {
    NSLog(@”开”);
    }else
    {
    NSLog(@”关”);
    }
    }

UIActivityIndicatorView

UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

activity.frame = CGRectMake(100, 200, 100, 50);

//开始转动
[activity startAnimating];

//设置颜色
activity.color = [UIColor redColor];

[self.view addSubview:activity];

UIProgressView

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIProgressView *progress = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 200, 300, 50)];

    //未走过的颜色
    progress.trackTintColor = [UIColor redColor];

    //走过的颜色
    progress.progressTintColor = [UIColor greenColor];

    [self.view addSubview:progress];

    //计时器让进度条走
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(progress:) userInfo:progress repeats:YES];
    }

  • (void)progress:(NSTimer *)time
    {
    UIProgressView progress2 = (UIProgressView )time.userInfo;

    //progress2.progress当前进度条的值
    [progress2 setProgress:progress2.progress+0.1 animated:YES];

    //进度条最大值为1
    if (progress2.progress == 1) {
    if ([time isValid]) {
    [time invalidate];
    }
    }
    }

UIActionSheet

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sheetShow)];
    [self.view addGestureRecognizer:tap];
    }

  • (void)sheetShow
    {
    //参数1:主标题 默认是灰色
    //参数2:代理对象
    //参数3:取消按钮
    //参数4:副标题 默认是红色
    //参数5:其他标题 蓝色
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@”取消” destructiveButtonTitle:nil otherButtonTitles:@”上海”,@”北京”,@”杭州”, nil];

    [sheet showInView:self.view];
    }

  • (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    NSLog(@”buttonIndex == %ld”,buttonIndex);
    NSLog(@”title == %@”,[actionSheet buttonTitleAtIndex:buttonIndex]);
    }

UIAlertView

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertShow)];
    [self.view addGestureRecognizer:tap];
    }

  • (void)alertShow
    {
    //参数1:主标题
    //参数2:发送的信息
    //参数3:代理对象
    //参数4:取消按钮
    //参数5:其他按钮
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”英雄联盟” message:@”您的账号已被封” delegate:self cancelButtonTitle:@”取消” otherButtonTitles:@”确定”, nil];

    [alert show];
    }

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    NSLog(@”index == %ld”,buttonIndex);
    // if (buttonIndex == 0) {
    //
    // }else
    // {
    //
    // }
    NSLog(@”title == %@”,[alertView buttonTitleAtIndex:buttonIndex]);
    }

UIStepper

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    _label = [[UILabel alloc] initWithFrame:CGRectMake(50, 190, 50, 50)];
    _label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];

    UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];

    [stepper addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
    // stepper.tintColor = [UIColor redColor];

    [self.view addSubview:stepper];
    }

  • (void)changeValue:(UIStepper *)stepper
    {
    //当前值
    NSLog(@”value == %g”,stepper.value);
    //步进数
    NSLog(@”stepValue == %g”,stepper.stepValue);

    _label.text = [NSString stringWithFormat:@”%g”,stepper.value];
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值