Does anybody know of a way to customize the appearance of the string based UISegmentedControl? I am trying to set the background color of the cell and the text color differently depending on the selected state of the item.
Alternatively, do you know of a way to create UIImages on the fly in which to include custom strings? (e.g. create UUImage with white background, overlay text, add to segmented control).
I know that you can only have strings or images in the segmented control...
To change the overall color of the segmented control use its backgroundColor . To change the color of the selected segment use selectedSegmentTintColor . To change the color/font of the unselected segment titles, use setTitleTextAttributes with a state of . normal / UIControlStateNormal .
setBlendMode(. multiply) ctx. draw(image. cgImage!, in: imageRect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } }
A segmented control is a linear set of segments, each of which functions as a button that can display a different view. Use a segmented control to offer choices that are closely related but mutually exclusive. A tab bar gives people the ability to switch between different subtasks, views, or modes in an app.
UISegmentedControl has a tintColor property -- this allows you to change what color the control is, but not the general "style" (the rounded, beveled shape):
segmentedControl.tintColor = [UIColor blueColor];
As for creating UIImages on the fly, you can create a CGContext, do whatever drawing you need to in that context (including strings), and then get a UIImage out of the context's CGImage:
CGContextRef drawContext = CGBitmapContextCreate(<many parameters>); //do drawing here CGImageRef finalImage = CGBitmapContextCreateImage(drawContext); UIImage *cellImage = [UIImage finalImage];
Please note, that if you use code like UIView.appearance().tintColor = .myColor
(or equiv. in ObjC), the effect most likely won't take place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With