UISegmentedControl-iOS

Posted 奉灬孝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

    

    }

 

}


以上是关于UISegmentedControl-iOS的主要内容,如果未能解决你的问题,请参考以下文章