OK I feel really weird for asking this question.. I started coding in Swift after SwiftUI and just never had to know this.. but..
How do I get the default behavior of a button.. where when I press it, the opacity changes (in UIKit). I thought it would be default behavior but obviously its not..
Here is what I have so far in my button configuration
private func configure() {
layer.cornerRadius = 10
setTitleColor(.white, for: .normal)
titleLabel?.font = UIFont.preferredFont(forTextStyle: .headline)
translatesAutoresizingMaskIntoConstraints = false
layer.borderWidth = 2
layer.borderColor = UIColor.systemPink.cgColor
}
The behavior you are describing is that of a .system UIButton as opposed to a .custom UIButton. If you create your button to be of type .system you get the proposed behavior for free. If the button is created by saying UIButton(), it is not a .system button.
Related
I encountered with an issue. I want to display 3 buttons, that are used for filtering my tableView. I found an appropriate images from “SF symbols.” Except one, which is the same I used for filtering(bigger to smaller) but is rotated on 180 degrees. I don’t know how to rotate bar button item, so I decided add custom button to bar button item.
There’s a problem occur – button is very small.
After adding button to bar button item:
I tried to use image configuration, but it’s not very good because it looks different and not with the same spacing.
After configuring button's image:
In the debug view hierarchy I found that symbol config is “Medium”, meanwhile other bar button items have “Body, Large”. But I haven’t found anything how to change it.
I have few questions:
Is there a way to flip bar button item without adding custom button in it?
If the way in the first question impossible, is this real to configure image “dynamically” the same way as other are displayed?
My code:
class viewController: UIViewController {
#IBOutlet var youngToOldfilterButton: UIBarButtonItem!
enter code here
override func viewDidLoad() {
let filterButton = UIButton(type: .custom)
var image = UIImage(systemName: "line.3.horizontal.decrease.circle", withConfiguration: UIImage.SymbolConfiguration(pointSize: 22))
filterButton.setImage(image, for: .normal)
filterButton.sizeToFit()
filterButton.addTarget(self, action: #selector(action), for: .touchUpInside)
//rotation transform
filterButton.transform = CGAffineTransform(rotationAngle: 180 * .pi/180)
youngToOldfilterButton.customView = filterButton
}
#objc func action {
...
}
}
symbol config is “Medium”, meanwhile other bar button items have “Body, Large”. But I haven’t found anything how to change it.
That way is to use a symbol configuration. Your problem is you are using the wrong configuration:
SymbolConfiguration(pointSize: 22))
Does that say Body, Large? No. You want this:
SymbolConfiguration(textStyle: .body, scale: .large)
https://developer.apple.com/documentation/uikit/uiimage/symbolconfiguration/3294246-init
However, the very best solution would likely be to design your own custom symbol image based directly on the "decreasing" image. This takes some time, but it isn't difficult, and you obviously care a lot about the exact thickness and position of the bars, so it might be worth it. Watch https://developer.apple.com/videos/play/wwdc2021/10250 for info.
I am creating an app for tvOS. I am trying to set the color of an UIButton but does not know how to get I t right.
var buttonA: UIButton(type: .system)
var buttonB: UIButton(type: .system)
...
buttonA.backgroundColor = UIColor.green
buttonB.backgroundColor = UIColor.red
I am happy with the result when the button is in focus but when the button is not in focus I get an ugly result. The green button has a colored layer that is missing its cornerRadius.
How do I color the buttons correctly?
buttonA.layer.cornerRadius = 10
These values are not customizable in instances of UIButton for tvOS out of the box. You have to create your custom solution in order to have your custom layout.
You can take a look to the implementation of this component for further details https://github.com/dcordero/FocusTvButton
I need to change the color of my button's text.
I also need to change the state to Disabled after the user presses it.
I have no idea how to do this. I've been looking things up for a while but they're all either in objective C or I can't understand it (usually help docs, they're stupid.).
In swift you change color for a specific State with the setTitleColor method.
In you case it will be :
button.setTitleColor(UIColor.grayColor, forState: UIControlState.Normal)
Swift 5 Update:
button.setTitleColor(UIColor.grayColor, for: UIControl.State.normal)
To change color of text
button.titleLabel.textColor = UIColor.grayColor()
To change state, on button press add following -
button.enabled = true
IBAction method should be like -
#IBAction func buttonTapped(sender : UIButton!) {
sender.enabled = false
}
Swift 3
button.setTitleColor(UIColor.gray, for: UIControlState.normal)
Note that;
grayColor has been renamed to gray
Normal is now normal (lowercase)
You have to set the text colour for the specific button state.
For Swift3, try below code :
#IBAction func butnClicked(sender : UIButton) {
sender.setTitleColor(.red, for: .normal)
sender.isEnabled = false
}
Set Enabled and text color from story board.
In iOS 6 and xcode 4, I had this:
https://dl.dropboxusercontent.com/u/60718318/photo.PNG
which was easily created with the code
[editButton setTintColor:[UIColor theGreenColor];
But on an iOS 7 phone, with the app built with the iOS 6 SDK using xcode 5, the edit button becomes un-tinted using this same exact code. (I can only post two images maximum, so just imagine that the edit button is the same color as the back button)
Many people both here on SO and all over the internet say that the only way to tint buttons now is to call
self.navigationController.navigationBar.tintColor = [UIColor theGreenColor];
which is also referenced in WWDC sessions. However, when I try something like this, I just get the following:
https://dl.dropboxusercontent.com/u/60718318/photo-2.PNG
which is not even close to correct. And according to the WWDC session I watched, this is supposed to tint BOTH buttons rather than just 1. How can I just tint 1 button, like I could in iOS 6 in xcode 4?
I don't think this is even possible anymore, I honestly think Apple just completely removed the functionality for no reason and without actually mentioning that they did. If anyone ever sees this question and wonders how I bypassed it, I just took a picture of the old button and used it as the image for the new button.
Thats certainly very unusual since you definitely can provide said functionality. The trick is to modify the tintColor of the UIBarButtonItem's UIButton rather than directly onto the item. Heres a naive implementation of a ViewController implementing this
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = createBarButtonItem()
}
func createBarButtonItem() -> UIBarButtonItem {
let button = UIButton(frame: CGRect(origin: CGPointZero, size: CGSize(width: 44, height: 44)))
button.setTitle("Press Me", forState: .Normal)
button.addTarget(self, action: "handleButtonTap:", forControlEvents: .TouchUpInside)
button.tintColor = UIColor.blueColor()
let buttonItem = UIBarButtonItem(customView: button)
return buttonItem
}
func handleButtonTap(button: UIButton) {
button.tintColor = UIColor.redColor()
}
}
Naturally you would want to manage the state of the buttons selection so you can toggle the color, but I leave that up to you
I have several UIButtons in a scrollview which I use in order to pass certain information. The information is saved in the title of each uibutton and when the button is clicked, it passes its title into the function.
All I want to do is hide the title of the button so you can not see the button. I have them overlaid over images which I use to show buttons. I have the text set to transparent but it still turns white when it is being clicked.
If you include code in your explanation, please explain where it should go.
After IOS7, If you want to just hide the title on titleLabel of a button you can do as follow. This way the title is still there it just makes it invisible. if you do NSLog("%#",button.currentTitle) you will see the title in terminal. Hope this helps.
[button setTitle:#"Button Title" forState:UIControlStateNormal];
button.titleLabel.layer.opacity = 0.0f;
I found only one correct working way:
//hide
yourButton.setTitleColor(UIColor.clearColor(), forState: .Normal)
//show (put your color)
yourButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
using button.titleLabel.hidden = YES will not work (at least on on iOS 7).
I ended up using:
// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
You can hide the label inside the button:
button.titleLabel.hidden=YES;
or set the button's title to #"" and save the value somewhere else when you want to retrieve it.
I create a subclass of UIButton and override layoutSubviews method. Hiding titleLabel in layoutSubviews method works.
public class LoadingButton: UIButton {
public var isTitleHidden: Bool = false {
didSet {
titleLabel?.isHidden = isTitleHidden
}
}
public override func layoutSubviews() {
super.layoutSubviews()
titleLabel?.isHidden = isTitleHidden
}
}
if wanna hide titleLabel, just set isTitleHidden = true
I could not remove the title from the titleLabel nor the whole view as I needed it for constraints.
I ended up using
isEnabled = false
titleLabel?.layer.opacity = 0
setTitleColor(.clear, for: .disabled)
to hide the title and
isEnabled = true
titleLabel?.layer.opacity = 1
setTitleColor(titleColor(for: .normal), for: .disabled)
to show it again
To hide a title temporary just setTitle to empty string
setTitle("", for: .normal)
the button title label will be hidden but the title will still in the titleLabel, you can return it back using
setTitle(titleLabel?.text, for: .normal)
If you want to temporary hide the title, while you disabling the button, use:
setTitle("Title", for: .normal)
setTitle("", for: .disabled)
Then, button.isEnabled = false, when you want to hide the title.
I got a problem with title, because used an attributed title and nothing above helped. Then i found a workaround:
button.titleEdgeInsets = .init(top: 0, left: shouldHide ? 1000 : 0, bottom: 0, right: 0)
However it has some disadvantages, but fit my needs.
I've come up with this solution, which allows you to set title label text and use it with button image without showing it and not moving button image to the left.
- (void)hideButtonLabel:(UIButton*)buttonInp {
buttonInp.titleLabel.layer.opacity = 0.0f;
uttonInp.titleLabel.font = [UIFont fontWithName:#"Helvetica-Light" size:0.0];
}
You cannot hide UIButton titleLabel using .hidden property. Instead you can do this.
To Hide:
[self.yourButton setTitle:nil forState:UIControlStateNormal];
To Show:
[self.yourButton setTitle:#"Your Text" forState:UIControlStateNormal];
In Swift-
You don't need to hide nor need to make the opacity to 0.0. Swift gave you a simpler way.
Just set the title as nil. In fact, I got the idea from the documentation.
Command click on the setTitle(_:for:) method and you will see-
open func setTitle(_ title: String?, for state: UIControl.State) // default is nil. title is assumed to be single line
So, I just set it to nil.
setTitle(nil, for: .normal)
Swift 5 to hide button label:
myButton.titleLabel?.isHidden = true
Here myButton is a #IBOutlet of the button.