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.
Related
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
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.
I'm trying to change UIButton's image to white and set a background color when it's pressed to end up with something like this:
I'm setting the button's image to a white one and setting it's background color like this:
button.setImage(UIImage(named: "Food_White")?.withRenderingMode(.alwaysOriginal), for: .highlighted)
button.setBackgroundImage(UIImage.imageWithColor(UIColor.gray, size: CGSize(width: 1.0, height: 1.0)), for: .highlighted)
But when I press the button the white image barely shows:
Does anybody know how to make the image less faint?
Are you using the default button type when creating them?
Try creating the button with .custom type instead of the default .system
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 :)
I'm using the MK pod (https://github.com/CosmicMind/MaterialKit) to try and place a fab button in my layout. The image I'm using is black. But the fab button appears to be overriding my icon color with a default color, because it shows up a faint blue color in the final fab button. Below are screenshots of the original image and the fab button with the image applied. Below are the only two lines I'm using to customize the fab button (I've subclassed it and made it IBDesignable, and the color I'm applying is one I've defined in a UIColor extension).
backgroundColor = UIColor.customBlueColor()
setImage(UIImage(named: "wifi"), forState: .Normal)
And here are the original icon and a screenshot of the result:
How do I keep the original icon color?
try using the tintColor property to adjust the icon color.
Example:
let img: UIImage? = UIImage(named: "wifi")
fabButton.setImage(img, forState: .Normal)
fabButton.setImage(img, forState: .Highlighted)
fabButton.tintColor = UIColor.blackColor()
Also, to avoid the black highlight color that occurs when you press the FabButton, set the image to .Highlighted, as well as, .Normal.