• UISegmentedControl-iOS


    //建立UISegmentedControl的数组

    NSArray *segmentedArray = [NSArray arrayWithObjects:@"线下培训",@"在线培训",@"模拟面试",@"Marlin面试",nil];

     

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];

    //设置样式 当为BorderedBartintColor才有效

    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

    segmentedControl.tintColor = [UIColor clearColor];

    //    文字选中的颜色

        [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:39/255.0 green:193/255.0 blue:180/255.0 alpha:1.0f]} forState:UIControlStateSelected];

    //   文字未选中的颜色

        [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];

        //背景未选中的颜色

        [segmentedControl setBackgroundImage:[WTImage imageWithColor:[UIColor colorWithRed:39/255.0 green:193/255.0 blue:180/255.0 alpha:1.0f] size:CGSizeMake(WIDTH, 44) alpha:1] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

        //背景选中的颜色

        [segmentedControl setBackgroundImage:[WTImage imageWithColor:[UIColor whiteColor] size:CGSizeMake(WIDTH, 44) alpha:1] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

        //设置圆角

        segmentedControl.layer.cornerRadius = 4;

        

        segmentedControl.clipsToBounds = YES;

    //设置frame

      segmentedControl.frame = CGRectMake(0.0, 0.0, 290, 30.0);

    //设置默认选中的index

        segmentedControl.selectedSegmentIndex = 0;

    [segmentedControl addTarget:self  action:@selector(indexDidChangeForSegmentedControl:)

                   forControlEvents:UIControlEventValueChanged];

        //方法1

        //[self.navigationController.navigationBar.topItem setTitleView:segmentedControl];

        //方法2

        [self.navigationItem setTitleView:segmentedControl];

    //segmentedControl选中方法

    -(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)Seg{

        

        NSInteger Index = Seg.selectedSegmentIndex;

        NSLog(@"Index %i", Index);

        switch (Index) {

            case 0:            

                [self selectmyView1];

                

                NSLog(@"%li",(long)Index);

                break;

            case 1:            

                [self selectmyView2];

                NSLog(@"%li",(long)Index);

                break;

            case 2:

                [self selectmyView3];

                NSLog(@"%li",(long)Index);

                break;

            case 3:            

                [self selectmyView4];

                NSLog(@"%li",(long)Index);

                break;

            default:

                break;

        

        }

     

    }

  • 相关阅读:
    Java测试代码及原理
    mysql性能调优
    markdown
    nginx
    触发器实际使用时容易碰到的几个小坑
    redis
    log4j
    Json
    导入工程报错The import android cannot be resolved
    mybatis小记
  • 原文地址:https://www.cnblogs.com/ansyxpf/p/7197394.html
Copyright © 2020-2023  润新知