How to change titleTextAttributes of UIBarButtonItem after it's pressed? - ios

I have a navigation bar with several UIBarButtons that I initially display with an alpha of 0.5, and would like to display with an alpha of 1 after pressed. I am storing these attributes in normalAttributes and highlightedAttributes.
I set up the buttons in the following manner:
colorButton = UIBarButtonItem(title: "Colors", style: .plain, target: self, action: #selector(showColors))
colorButton.setTitleTextAttributes(normalAttributes, for: .normal)
colorButton.setTitleTextAttributes(highlightedAttributes, for: .selected)
This leads to the button briefly switching to the highlightedAttributes, which is expected. However, within the button's action showColors I then perform the following:
textButton.setTitleTextAttributes(highlightedAttributes, for:[])
I have also tried using .normal instead of []. Neither method seems to have any sort of effect on the button. Any help with this would be really appreciated.

The easiest way to accomplish this I can think of is using a custom view. Meaning, create a separate UIButton and make a reference to it in your class (e.g., let yourButton = UIButton()). Initialize your bar button item with UIBarButtonItem(customView: yourButton) and then use it however you want to.
Just note that you'll need to add your action/target pair on yourButton, not on the bar button item. Then in your action handler, showColors in your case, you can change the appearance of yourButton.
You can either toggle alpha directly or if it's just text we're talking about, you could do:
yourButton.setTitleColor(yourColor, for: .normal)
and
yourButton.setTitleColor(yourColorHalfAlpha, for: .selected).
Then in your showColors, just call yourButton.isSelected = !yourButton.isSelected to toggle the appearance.

A UIButton contains a UILabel as the titleLabel property. Just set its attributes the usual way instead of setting them via the button’s pass-through functions.

Related

How to make some buttons of tableView cells clickable and some unclickable | iOS

I have a tableView in which each cell contains a button with different titles.
Actually I am passing a titles array to button.
cell.applyButton.setTitle(titleValues[indexPath.row], for: .normal)
Now I want to make only those buttons clickable which have title schedule. Please help.
Set the button's .isEnabled property, based on the title:
cell.applyButton.setTitle(titleValues[indexPath.row], for: .normal)
cell.applyButton.isEnabled = titleValues[indexPath.row] == "Schedule"

UIButton highlight color overlaps text when the button is pressed

This is a normal state of my UIButton:
But when I press on the UIButton, its highlight color should be changed to a different one. And here is what I have:
So, if you can notice, my white text becomes overlapped with the new color. But what I should have as a result is just a different color of the highlight and always the white text. Like so:
What I am doing so far is:
In Attribute Inspector in my xib file I changed the Highlight Color to a new one.
I also use updateButton.setTitleColor(.white, for: .highlighted) in my code but it doesn't help.
I've also taken a look at this question: UIButton background color overlaps text on highlight but the accepted answer didn't help much.
Similar to myButton.setTitleColor(.white, for: .normal) please try setting title colour for .highlighted state. That should work for you.
You can refer:
https://developer.apple.com/documentation/uikit/uibutton/1623993-settitlecolor
This myButton.setTitleColor(.white, for: .normal) doesnt change title color when highlighted , Its for normal state. If you want to change textColor when button is highlighted, you need to use myButton.setTitleColor(.white, for: .highlighted)
Also when you change state config in storyboard
you can set Text Color
If its a custom Button you can override isHighlighted method and do what you need like
class MyButton : UIButton {
override var isHighlighted: Bool{
didSet {
tintColor = isHighlighted ? UIColor.red : UIColor.white
backgroundColor = UIColor.blue
.....
// do what you need
}
}
As your button is subclass of UIView class CellButton: UIView {}, you need to handle touches begin and end events in custom CellButton and handle the appearance of the view. If it was subclass of UIButton then it would have handled automatically for you :)
You can also try with gestures, for more info you can refer: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_tap_gestures

Swift Changing UISlider images and tint doesn't work as expected

I have a slider in my ViewController:
#IBOutlet weak var timeSlider: UISlider!
When using the code:
timeSlider.setMinimumTrackImage(#imageLiteral(resourceName: "track"), for: .normal)
timeSlider.setMaximumTrackImage(#imageLiteral(resourceName: "track"), for: .normal)
timeSlider.setThumbImage(#imageLiteral(resourceName: "thumb2"), for: .normal)
My slider looks like this:
It works great and behaves as one would expect it to behave
Now I want to make the part before thumb have different color and the part after to also have different color, so I created a function which creates new images as templates (templates being an array) from images I use for my view:
func setTemplates(){
templates.removeAll()
for i in 0 ..< 10{
templates.append(playImg[i].withRenderingMode(.alwaysTemplate))
}
}
templates[5] is the same image as "thumb2"
templates[6] = "track"
and then I wanted to assign template[6] (which the image "track" converted to template) and color it in the same fashion I do with other images in the view like buttons, or to color labels etc. like this:
timeSlider.setMinimumTrackImage(templates[6], for: .normal)
timeSlider.setMaximumTrackImage(templates[6], for: .normal)
timeSlider.setThumbImage(templates[5], for: .normal)
timeSlider.minimumTrackTintColor = colors.primaryColor
timeSlider.maximumTrackTintColor = colors.detailColor
timeSlider.thumbTintColor = colors.secondaryColor
Where colors is a struct holding four different colors. But as you can see it doesn't work properly
The image thumb is being set to default one, it doesn't even behave properly as I can't move the thumb to 0.0 position of the slider and after I move it or the colors change, the part after thumb gets blue color which is default for slider.
I tried different ways to fight this problem but this is the closest I got it to working properly. At this point I'm starting to think it's a bug but maybe I made some mistake in my code. As I said I have buttons, labels, which I color the same way I tried here
button.setImage(templates[7], forState: .normal)
button.tintColor = colors.primaryColor
and it works
What am I doing wrong?
From the documentation for UISlider thumbTintColor:
Setting this property resets the thumb images back to the slider’s default images; any custom images are released by the slider. Setting this property to nil resets the tint color to the default and removes any custom thumb images.
Same for the two track tints.
If you want custom images then just set the custom images without also setting the tints.
In other words, set either the custom images or set the tints. Do not set both.

UITableViewRowAction customise title font size in Swift

I have an edit and a delete tableViewRowAction in my tableViewCell. Currently I'm using the built in emojis to use as my title, but it is too small. How do I make the font larger?
I know we can only customise a limited amount of things in the tableViewRowAction. But is there a way to go around it to make just the title font bigger?
I checked other threads and most of them used:
UIButton.appearance().setAttributedTitle(NSAttributedString(string: "Your Button", attributes: attributes), forState: .Normal)
with a set attribute that determines the font size of course. However, this affects ALL buttons, and that isn't good.
Any help is much appreciated! Thanks!
you are setting this to UIButton so it is set for all UIButton objects.
You can set it for specific button object like,
let myButton: UIButton = UIButton() //your button here
let attributedStr: NSAttributedString = NSAttributedString()
myButton.setAttributedTitle(attributedStr, forState: .Normal)
Update :
You can set title as attributed string.
check Apple documentation for that and refer this answer for more details
Hope this will help :)

Creating Default-Style UIButton in Swift

When I create a UIButtonin Swift, e.g. by
let aButton = UIButton()
aButton.setTitle("Title", forState: UIControlState.Normal)
it seems like I'm not getting the same button style as I would get if I added a button in Interface Builder. Specifically, the title color is white instead of the default "tint color", leaving the button invisible. Now I want this button to look like any other button, so I don't want to specify my own colors.
Setting the normal (not pressed) state can be done with:
aButton.setTitleColor(self.tintColor, forState: UIControlState.Normal)
So far, so good. But the text color does not change when the button is pressed. I guess, for that I need
aButton.setTitleColor(/* somehting here */, self.tintColor, forState: UIControlState.Highlighted)
Ideally without hardcoding the color, what do I need to substitute above to get the default pressed button color? Or is there even a simpler way to just create a UIButton with default style?
In order to declare a button of the same (default) style as in the storyboard/interface builder, use the following code to instantiate it:
let aButton = UIButton(type: .system)
Otherwise the button is of type custom.
You can read about the various button types here.

Resources