I am using the SevenSwitch control instead of the built in UISwitch. Instead of an on and off mode I want both sides to have the same background color.
I tried the following code:
let mySwitch = SevenSwitch()
let switchTintColor = UIColor(red: 109/255, green: 59/255, blue: 100/255, alpha: 1.0)
mySwitch.offLabel.backgroundColor = switchTintColor
mySwitch.onTintColor = switchTintColor
mySwitch.borderColor = switchTintColor
That gets really close to what I want except for the background behind the thumb view when the switch is off.
You'll want to set the activeColor and inactiveColor properties on the switch. That should get you what you're after.
mySwitch.activeColor = switchTintColor
mySwitch.inactiveColor = switchTintColor
Related
When I change the background color of textfield inside UISearchBar, I see that the color is not correctly set. For example: I set color #2B2F6A, but it changes to #343869.
Any suggestions for this? Thanks a lot.
Code:
iOS < 13
if let textField = self.value(forKey: "searchField") as? UITextField {
if let backgroundView = textField.subviews.first {
backgroundView.backgroundColor = UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0)
}
}
iOS 13:
self.searchTextField.backgroundColor = UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0)
the set color likely grey but the result look like black
On iOS13, even though the UISearchBar property searchTextField of type UISearchTextField is a UITextField subclass and in the related session of WWDC there is a claim that developers can customize the textField property just like they can with a UITextField, there are some properties that do not keep respect this "contract". For example changing rightView is not customisable. Presumably this is also the case with backgroundColor.
Update: I had a check, I can set the backgroundColor without any issue on iOS13 - are you using the searchTextField property?
I have the following subclass:
class GeneralNavigationBar: UINavigationBar {
override func layoutSubviews() {
super.layoutSubviews()
self.barTintColor = UIColor(rgb: 0x2A5298) //Extension that converts hex to color
self.tintColor = UIColor.white
}
}
I want the title of the bar to be white. When I apply this class to a NavigationBar in the storyboard, the background gets blue (hex, as he is supposed to), but the title remains black.
It's strange, since you can alter the color of the bar in the ViewController it appears in:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
This works.
Try appearance,see how..
var navbarappearace = UINavigationBar.appearance()
navbarappearace.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0)
navbarappearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
Put the above code in AppDelegate, it should affect the whole project.
Cheers.
I am working on tvOS project, I need to create custom alert view with particular background color and text.
Finally , I have got the solution,
if let next = context.nextFocusedView as? UIButton {
next.setTitleColor(UIColor.blackColor(), forState: UIControlState.Focused)
next.backgroundColor = UIColor(red: 248/255, green: 175/255, blue: 2/255, alpha: 1.0)
How can I get code or anything about selected color to set it anywhere programmatically?
I'd like to get background-color information
Get background color from UI object:
let color = label.backgroundColor
Set background color for UI object:
// green color for example
let color = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha 1.0)
label.backgroundColor = color
Get red, green, blue and alpha values from UIColor:
let color = label.backgroundColor
var red = CGFloat()
var green = CGFloat()
var blue = CGFloat()
var alpha = CGFloat()
color!.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
By getting UIView's backgroundColor attribute:
If you don't want to use code to retrieve the value you can click the blue icon (arrows) on the right and choose other at the bottom of the list. Then you'll be able to see the color's values in a variety of ways.
I have a MainView Controller and a second SettingsView Controller.
In SettingsView controller, I let user select a background color, save it and move back to MainView Controller.
I use Navigation controller segue to move from Main to Settings and use dismissViewControllerAnimated to move back to Main.
My problem is when I set background for main view, it doesn't show up.
But if I close and restart app then it comes up correctly.
Is it because Main view is already open and not refreshed back?
Here is the code:
Settings:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject("theme", forKey: "BackgroundColor")
Main:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clearColor()
var backgroundLayer = Util.GetTheme()
backgroundLayer.frame = view.frame
view.layer.insertSublayer(backgroundLayer, atIndex: 0)
}
As per your requirement, you need to move the code for changing color to viewWillAppear method. Since viewDidLoad will be called only once at the time when it get loaded to memory. viewWillAppear will be called each time it will come to foreground or visible to user.
But where you are saving selected color and applying the saved color?
Try This code :
class Colors {
let colorTop = UIColor(red: 192.0/255.0, green: 38.0/255.0, blue: 42.0/255.0, alpha: 1.0).CGColor
let colorBottom = UIColor(red: 35.0/255.0, green: 2.0/255.0, blue: 2.0/255.0, alpha: 1.0).CGColor
let gl: CAGradientLayer
init() {
gl = CAGradientLayer()
gl.colors = [ colorTop, colorBottom]
gl.locations = [ 0.0, 1.0]
}
}