I get gradient image with this method
func gradient(size:CGSize,color:[UIColor]) -> UIImage?{
//turn color into cgcolor
let colors = color.map{$0.cgColor}
//begin graphics context
UIGraphicsBeginImageContextWithOptions(size, true, 0.0)
guard let context = UIGraphicsGetCurrentContext() else {
return nil
}
// From now on, the context gets ended if any return happens
defer {UIGraphicsEndImageContext()}
//create core graphics context
let locations:[CGFloat] = [0.0,1.0]
guard let gredient = CGGradient.init(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: colors as NSArray as CFArray, locations: locations) else {
return nil
}
//draw the gradient
context.drawLinearGradient(gredient, start: CGPoint(x:0.0,y:size.height), end: CGPoint(x:size.width,y:size.height), options: [])
// Generate the image (the defer takes care of closing the context)
return UIGraphicsGetImageFromCurrentImageContext()
}
Then I set tintColor of segmented control to gradient:
let gradientImage = gradient(size: listSegmentedControl.frame.size, color: [UIColor.black, UIColor.red])!
listSegmentedControl.tintColor = UIColor(patternImage: gradientImage)
and that doesn't work. However, same code works for setting backgroundColor:
let gradientImage = gradient(size: listSegmentedControl.frame.size, color: [UIColor.black, UIColor.red])!
listSegmentedControl.backgroundColor = UIColor(patternImage: gradientImage)
Does anybody have any ideas why? I really need to set gradient tintColor. Any help is very appreciated.
EDIT:
Ideally I want my segmented control to look like this:
This is a known hack to change the tint color of
UISegmentedControl
Though looks like a ugly hack, I have been using it from a quite a while and seems fairly straight forward. Hope it helps.
EDIT:
Is this what you need buddy?
If yes lemme know, Ill post the code for the same.
EDIT 2:
As OP has mentioned in his comment that the output he is expecting is same as the one I showed in image above, providing code for the same.
Disclaimer:
As mentioned by rmaddy in his comments below, this is a hack and makes use of undocumented (Complete public API though) but a very well known hack to change the tint color of UISegemntedControl that exists from as far as iOS 5 (Thats how I remember, lemme know if I am wrong )
So use answer with the caution in mind that, in future releases of iOS Apple might change the structure of subviews in
UISegemntedControl
and might affect your O/P. Nothing that I can see, will result in crash but might affect the way O/P is rendered on screen.I have declared a variable so that GradientImage can be generated only once, but its up to your implementation to use it the way you want
In
ViewDidLoad
I initialize thegradientImage
andUISegmentedControl
asFinally updateGradientBackground function definition is same as the one I posted in my original answer
Finally, in IBAction of UISegmentedControl, simply call
Hope this helps