设置backgroundImage后,UISegmentedControl tintcolor无法工作

时间:2016-12-08 03:06:00

标签: ios uisegmentedcontrol

当我们不改变UISegmentedControl的背景图像时,设置tintcolor可以完美地工作。但是一旦我们改变背景图像,无论我们为tintcolor设置哪种颜色,UIsegmentControl选择的颜色都将以灰色显示。如何在不自定义新类的情况下解决此问题?

1 个答案:

答案 0 :(得分:0)

使用此:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.segmentedControl setBackgroundImage:[UIImage imageNamed:@"blue.png"]
                                     forState:UIControlStateNormal
                                   barMetrics:UIBarMetricsDefault];

    [self.segmentedControl setBackgroundImage:[UIImage imageNamed:@"green.png"]
                                     forState:UIControlStateSelected
                                   barMetrics:UIBarMetricsDefault];
    self.segmentedControl.tintColor = [UIColor orangeColor];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

输出:

enter image description here

GitHub链接:

https://github.com/k-sathireddy/SegmentedControlBackgroundWithTint