I have an alert controller that I set to a new tint color with this line of code:
alertController.view.tintColor = UIColor(red: 0.59, green: 0.59, blue: 0.59, alpha: 1.0)
The color does show correctly, but once the alert action is tapped, it changes back to the default blue color automatically. How can I make it so that the tint color does not change back to default. I have heard of the func tintColorDidChange() but I am unsure of how to use it?
You can try this. It has been discussed here
you should add the same code in presentViewController completion handler
presentViewController(alert, animated: true) {
alert.view.tintColor = UIColor(red: 0.59, green: 0.59, blue: 0.59, alpha: 1.0)
}
Related
I have a floating button in my app that display 5 different buttons. Each of them has a color but initially are gray. When the user selects one of the buttons, all of them hide, and when the user taps again on the floating button, the button that was previously tapped is not gray but with its proper color.
Also the logic would be that the user can tap another one of the buttons to select it, and the previous one would be unselected, as if the user taps on a selected button, it gets unselected.
I'm using cocoa pods for the Floating Button, JJFloatingActionButton, but on the documentation there is no info about this.
func configureActionButton() {
actionButton.overlayView.backgroundColor = UIColor(white: 0, alpha: 0.5)
actionButton.buttonImage = UIImage(systemName: "line.horizontal.3.decrease")
actionButton.buttonColor = UIColor(named: "signoColor")!
actionButton.buttonImageColor = .white
actionButton.buttonImageSize = CGSize(width: 30, height: 30)
actionButton.itemAnimationConfiguration = .slideIn(withInterItemSpacing: 14)
actionButton.itemSizeRatio = CGFloat(0.75)
let item = actionButton.addItem()
item.titleLabel.text = "Button 1"
item.buttonColor = UIColor(red: 254/255.0, green: 224/255.0, blue: 255/255.0, alpha: 1.0)
item.buttonImageColor = UIColor(red: 200/255.0, green: 73/255.0, blue: 203/255.0, alpha: 1.0)
item.imageSize = CGSize(width: 30, height: 30)
item.action = { item in
//This is for the functionality of the button
}
let item2 = actionButton.addItem()
item2.titleLabel.text = "Button 2"
item2.buttonColor = UIColor(red: 254/255.0, green: 224/255.0, blue: 255/255.0, alpha: 1.0)
item2.buttonImageColor = UIColor(red: 200/255.0, green: 73/255.0, blue: 203/255.0, alpha: 1.0)
item2.imageSize = CGSize(width: 30, height: 30)
item2.action = { item in
//This is for the functionality of the button
}
//And I have 3 more buttons
}
This is how the floating button looks like:
I would appreciate some help. I've searched for similar questions but none of them solved this problem. Thank you!
I would like to change the text color of my UIBarButtonItem.
Current color is "system blue"
I tried this:
let button = UIBarButtonItem(title: "Button", style: .plain, target: self, action: #selector(exit))
button.tintColor = UIColor(red: 126.0, green: 189.0, blue: 11.0, alpha: 1.00)
The rgb values should show a green color - but the button text color shows white.
Where is my fault ?
You have to set values between 0 to 1 for RGB Colors. Use the following
UIColor(red: 126.0/255.0, green: 189.0/255.0, blue: 11.0/255.0, alpha: 1.00)
OR
UIColor(red: 0.49, green: 0.74, blue: 0.04, alpha: 1.00)
So I've got these buttons (3 stacks of 6, so 18), and what I want to achieve is when I press one of these buttons :
border color & text color changes
the other buttons are reset to their normal styling
But I don't want to disable the others via a "isEnabled" trick (I've only found solutions here involving isEnabled), I still want them to be enabled, I just want them not to be "highlighted" with my custom styling when one is pressed.
for the first part which is just the styling I did this inside the IBAction :
#IBAction func preset1Pressed(_ sender: UIButton) {
preset1.layer.borderColor = #colorLiteral(red: 0.4095217415, green: 0.6107917746, blue: 0.2774988226, alpha: 1)
preset1.layer.borderWidth = 0.42
preset1.setTitleColor(#colorLiteral(red: 0.4095217415, green: 0.6107917746, blue: 0.2774988226, alpha: 1), for: .normal)
}
This small portion was just for 1 button, but I guess if I have 18 of them I should make a class or a struct rather than copy this inside each IBAction ?? Or a func ?
Then for the 2nd part I'm not sure about how to do it, reversing to the original properties (set in the Attributes Inspector) of the other buttons when one is pressed.
Intuitively I'm sure it's a combination of an array of all the 18 buttons inside a function that would loop through all the array, and maybe make a bool on each button to check if they are pressed or not, but I really don't know how the syntax would be...
Worth noting also that if I press twice on the same button I don't want it to reverse to its original properties but to keep the "pressed" styling.
Thanks in advance !
Define UIButton array and add buttons when you add it to stack.set index as button tag (0-17)
fileprivate var btnArray:[UIButton] = []
btn0.tag = 0
btnArray.appent(btn0)
Then you can change style in button click function like this
#IBAction func preset1Pressed(_ sender: UIButton) {
btnArray.forEach { (button) in
if button.tag == sender.tag {
preset1.layer.borderColor = #colorLiteral(red: 0.4095217415, green: 0.6107917746, blue: 0.2774988226, alpha: 1)
preset1.layer.borderWidth = 0.42
preset1.setTitleColor(#colorLiteral(red: 0.4095217415, green: 0.6107917746, blue: 0.2774988226, alpha: 1), for: .normal)
view.layoutIfNeeded()
}else{
//add default style
view.layoutIfNeeded()
}
}
}
Don't frogot to add view.layoutIfNeeded() after this
So what suggested Dilan did not completely solve it on its own, though I experimented with it and it actually helped me solve it, apart I did these modifications :
I created my Array of buttons (called presetsArray) and tagged them.
Then I created a function to be called in all IBActions, but as sender.tag wouldn't work here's how I worked out the function :
func styleButtons(tag: Int) {
let tag = tag
presetsArray.forEach{ (button) in
if button.tag == tag {
button.layer.borderColor = #colorLiteral(red: 0.4095217415, green: 0.6107917746, blue: 0.2774988226, alpha: 1)
button.layer.borderWidth = 0.42
button.setTitleColor(#colorLiteral(red: 0.4095217415, green: 0.6107917746, blue: 0.2774988226, alpha: 1), for: .normal)
view.layoutIfNeeded()
} else {
button.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
button.setTitleColor(#colorLiteral(red: 0.4978774786, green: 0.5020093918, blue: 0.5019439459, alpha: 1), for: .normal)
}
}
}
Then in each IBAction for preset X I would call it like this :
styleButtons(tag: presetX.tag)
In the else section of my function as view.layoutIfNeeded() wouldn't reverse back to my original styling I just went for the color picker and picked my original styling, not the most legit way to do this I feel but it works !
Thanks a lot Dilan for the help.
I am trying to animate UILabel textColor changes using UIView.animate. However, nothing is changing when I attempt to use UIView. Here is my code:
titleLabel.textColor = UIColor(red: 104/250, green: 155/250, blue: 121/250, alpha: 1)
UIView.animate(withDuration: 1.0, animations: {
self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
I expect the color of the label to change to a greenish color and then change back to the gray-ish color it was before. Thanks in advance for helping!
The textColor property is not specified as being animatable in Apple developer docs, so I don't think you can do it with a simple UIView animations block.
You can use UIView transition instead.
Code:
UIView.transition(with: self.titleLabel, duration: 1.0, options: .transitionCrossDissolve, animations: {
self.titleLabel.textColor = UIColor(red: 104/250, green: 155/250, blue: 121/250, alpha: 1)
}, completion: { _ in
self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
})
I hope it will help you. Let me know if you are still having any issue.
try
UIView.animate(withDuration: 1.0, animations: {
self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0)
self.view.layoutIfNeeded()
}
TextColor is not supposed to be animatable in the documentation so you have to perform below animation code.
Reason:
The reason that textColor is not animatable is that UILabel uses a regular CALayer instead of a CATextLayer. so you have two options
perform animation on CATextLayer
Add custom animation layer as given below in the code.
let changeColor = CATransition()
changeColor.duration = 1
CATransaction.begin()
CATransaction.setCompletionBlock {
self.titleLabel.layer.add(changeColor, forKey: nil)
self.titleLabel.textColor = UIColor(red:206/255, green: 206/255, blue: 206/255, alpha: 1.0) }
titleLabel.textColor = UIColor(red: 104.0/255.0, green: 155.0/255.0, blue: 121.0/255.0, alpha: 1.0)
CATransaction.commit()
This question already has answers here:
UIColor not working with RGBA values
(6 answers)
Closed 6 years ago.
In a view controller embedded in a navigation controller I am attempting to change the barTintColor to a custom color. What I have experienced is if I use a default color such as the one below, the color is actually applied:
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
However when I try to create an instance of my own custom color such as this code would show, the color is not applied:
let customRedColor = UIColor( red: 255, green: 0, blue: 13, alpha: 1 )
self.navigationController?.navigationBar.barTintColor = customRedColor
I am very curious as to why the custom color is not being applied to the navigation bar so that leads me to ask:
What approach should be taken in order to correctly apply a custom color to the navigation bars barTintColor property.
You need to give color by dividing them by 255, the syntax is like this.
init(red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
Each of the ones that take a CGFloat take a value between 0.0 and 1.0, referring to either the complete absence of, or the maximum amount of that color component respectively. So, this means that even if you have pure RGB values in decimal or hexadecimal format, you will have to divide them by decimal 255 to get the amount to input here.
let customRedColor = UIColor(red: 255/255.0 , green: 0, blue: 13/255.0, alpha: 1.0)
//or Direct
let customRedColor = UIColor(red: 1.0 , green: 0, blue: 0.05, alpha: 1.0)
self.navigationController?.navigationBar.barTintColor = customRedColor