首页 > 代码库 > UISegmentedControl-iOS

UISegmentedControl-iOS

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } span.s1 { color: #703daa } span.s2 { font: 14.0px Menlo; color: #000000 } span.s3 { color: #3d1d81 } span.s4 { } span.s5 { font: 11.0px "PingFang SC" } span.s6 { color: #bb2ca2 } span.s7 { font: 14.0px Menlo }</style>

//建立UISegmentedControl的数组

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

 

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

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } span.s1 { } span.s2 { font: 11.0px "PingFang SC" } span.s3 { font: 11.0px Menlo; color: #703daa } span.s4 { font: 11.0px Menlo; color: #3d1d81 }</style>

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

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } span.s1 { font: 14.0px Menlo; color: #000000 } span.s2 { color: #703daa } span.s3 { }</style>

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

segmentedControl.tintColor = [UIColor clearColor];

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } span.s1 { } span.s2 { font: 11.0px "PingFang SC" } span.s3 { font: 14.0px Menlo; color: #000000 } span.s4 { color: #272ad8 } span.s5 { color: #703daa }</style>

//    文字选中的颜色

    [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];

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px } span.s1 { font: 14.0px Menlo; color: #000000 } span.s2 { font: 11.0px Menlo } span.s3 { } span.s4 { color: #4f8187 } span.s5 { color: #31595d } span.s6 { color: #703daa } span.s7 { color: #272ad8 } span.s8 { color: #78492a } span.s9 { color: #000000 } span.s10 { font: 11.0px "PingFang SC" } span.s11 { font: 11.0px Menlo; color: #703daa } span.s12 { font: 11.0px Menlo; color: #272ad8 } span.s13 { font: 11.0px Menlo; color: #bb2ca2 }</style>

    //背景未选中的颜色

    [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

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } span.s1 { } span.s2 { font: 11.0px Menlo; color: #703daa } span.s3 { font: 11.0px Menlo; color: #3d1d81 } span.s4 { font: 11.0px Menlo; color: #272ad8 }</style>

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

//设置默认选中的index

    segmentedControl.selectedSegmentIndex = 0;

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3d1d81 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400 } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } span.s1 { } span.s2 { font: 11.0px Menlo; color: #3d1d81 } span.s3 { font: 11.0px Menlo; color: #bb2ca2 } span.s4 { font: 14.0px Menlo; color: #000000 } span.s5 { color: #000000 } span.s6 { font: 11.0px Menlo } span.s7 { font: 11.0px "PingFang SC" } span.s8 { font: 14.0px Menlo } span.s9 { color: #bb2ca2 } span.s10 { color: #703daa } span.s11 { color: #3d1d81 }</style>

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

               forControlEvents:UIControlEventValueChanged];

    //方法1

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

    //方法2

    [self.navigationItem setTitleView:segmentedControl];

//segmentedControl选中方法

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo } span.s1 { } span.s2 { font: 11.0px Menlo; color: #bb2ca2 } span.s3 { font: 11.0px Menlo; color: #703daa } span.s4 { font: 14.0px Menlo; color: #000000 } span.s5 { font: 14.0px Menlo } span.s6 { color: #78492a } span.s7 { color: #d12f1b } span.s8 { font: 11.0px Menlo; color: #272ad8 } span.s9 { font: 11.0px Menlo; color: #4f8187 } span.s10 { font: 11.0px Menlo; color: #d12f1b } span.s11 { color: #bb2ca2 } span.s12 { color: #31595d } span.s13 { font: 11.0px Menlo; color: #78492a }</style>

-(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;

    

    }

 

}

UISegmentedControl-iOS