Referencing a UIButton by tag value - swift

I am aware that a pushed button can be updated by reference to sender.
However I want to access a button from the program body by reference to its tag and then update its label,if that is possible.
Suggestions on how to do this (in Swift) would be appreciated.

To get a button inside a view using its tag, you can use the UIView.viewWithTag function.
if let button = self.view.viewWithTag(tag) as? UIButton
{
button.setTitle("newTitle", forState: UIControlState.Normal)
}

You have to use viewWithTag
ex:
var button = self.view.viewWithTag(tagNumber) as UIButton
button.setTitle("Button Title", forState: UIControlState.Normal)

Set the tag for all buttons. Then have a property
var buttons = [UIButton]
and in your awakeFromNib init it
buttons.append[myButtonWithTag0]
buttons.append[myButtonWithTag1]
// etc
Now you can access your buttons via
let button = buttons[index]

Swift3 : Possible with viewWithTag(button_tag) as:
let tmpButton = self.view.viewWithTag(tag) as? UIButton
tmpButton?.setTitleColor(.red, for: .normal)
tmpButton?.setTitle("Button Title", for: .normal)

Related

UIButton doesn't work in child view controller

UIButton gestures doesn't recognize in child view controller.
UIButton add targer work (button get target action)
User interaction is turned on everywhere, button size is okay. I try to set childVC as main, and then all work good, debug view hierarchy said that button view over other elements. IDK where is problem. I can provide more code, just tell me.
Add target code:
view.buttonOfLanguageFromTranslate.addTarget(self, action: #selector(self.openDetailView(_:)), for: .touchDown)
Add child VC code:
let childVC = ChildVC()
view.addSubview(childVC.view)
self.addChild(childVC)
childVC.didMove(toParent: self)
childVC.view.translatesAutoresizingMaskIntoConstraints = false
self.view.isUserInteractionEnabled = true
self.childVC = childVC
Button initialization:
var button: UIButton = {
let button = UIButton()
button.setTitle("Title", for: .normal)
button.setTitleColor(.black, for: .normal)
button.setTitleColor(.green, for: .selected)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
The problem was with
childVC.translatesAutoresizingMaskIntoConstraints = false
(in mainVC)
When i remove it, childVC recognise my tap
and i dont set additional size for childVC, view of childVC is display correct but not childVC. (as i think)

How to programmatically add tags to a UIButton in Swift

I know how to add tags in main.storyboard files, but now I am working on creating everything programmatically. However, I couldn't find anything online that tells me how to add tags to a button. I tried to read the apple documentation but don't know how to implement it.
Here is for example how I created my button, how can I add a tag to it, so that when I make multiple buttons and want to link them to a single action I can know what button has been pressed?
let buttonOne() : UIButton {
let button = UIButton(type: .system)
button.backgroundColor = .red
return button
}()
Try this:
let buttonOne() : UIButton {
let button = UIButton(type: .system)
button.backgroundColor = .red
button.tag = 2
return button
}()
Declare The Button
let menuChooseButton = UIButton()
menuChooseButton.frame.origin.y = 0
menuChooseButton.frame.origin.x = xPosition
menuChooseButton.frame.size.width = subViewWidth
menuChooseButton.frame.size.height = subViewHeight
menuChooseButton.backgroundColor = .clear
menuChooseButton.setTitle("", for: .normal)
menuChooseButton.tag = i
menuChooseButton.addTarget(self, action: #selector(menuSelectAction), for: .touchUpInside)
menuScrollView.addSubview(menuChooseButton)
Define Button Action
#objc func menuSelectAction(sender: UIButton!){
var tagNo: Int = sender.tag
if tagNo == 0{
// Whatever you want
}
}

How to loop through buttons

I have 16 buttons named like btn1, btn2 etc.. how can i loop through this and set an value to each button?
I have this code:
//set values on the button
for i in 1..<17{
var mybutton = "btn" + String(i);
println(mybutton)
mybutton.setTitle(String(number), forState: UIControlState.Normal)
}
}
but that doesn't work because the software sees it as an string...
How can i solve this issue?
You can create array of buttons:
let array = [btn1, btn2, btn3 ...]
for i in array.count
{
//do something
array[i].setTitle(String(number), forState: UIControlState.Normal)
}
But if you have a lot of buttons in Interface Builder, the better way to create them programmatically.
In addition to the answer of ChikabuZ:
If you separately added the buttons in storyboard, I suggest you to create an outlet collection instead: #IBOutlet var buttons: [UIButton]!.
CTRL drag all those buttons in this outlet and you can access the array like ChikabuZ has suggested.
I personally prefer to use a map instead:
buttons.map { $0.setTitle(String(number), forState: .Normal) }
If you added these in the interface builder and have a reference to the superview, you can assign each of them a tag from 1 to 16 access them using the method "viewWithTag":
//set values on the button
for i in 1..<17{
var sView = [REFERENCE TO YOUR SUPERVIEW HERE]
var mybutton = sView.viewWithTag(i) as! UIButton?
println(mybutton)
mybutton.setTitle(String(number), forState: UIControlState.Normal)
}
}

UIButton setTitle doesn't work

I'm trying to change the text of a UIButton when it gets clicked. I've tried the following things, which failed:
First I tried inserting a button on the storyboard and linking it to the view controller, with the code for the button appearing like this:
#IBAction func button(sender: AnyObject) {}.
Within the button's parentheses, I then used
button.setTitle ("something" , for State: .Selected)
However, when I run this code in the simulator, it doesn't seem to work at all.
I have also tried, following the Apple reference manual, to set different labels for different states in the side menu after clicking on said button, but still this didn't work.
Can anyone tell me exactly (i.e. what code to write and where exactly it goes) how to make this happen?
The type of the sender should be UIButton, when creating the IBAction function.
The sender is your button. The function is called when you tap on the button. You must use the function setTitle on the sender(your button).
#IBAction func buttonTouchedUpInside(sender: UIButton) {
sender.setTitle("buttonName", forState: .normal)
}
Swift 4:
Plain Title
The setTitle() method will work for titles that are "Plain" as defined in the button's Attributes inspector.
#IBAction func button(sender: UIButton) {
sender.setTitle("buttonName", for: .normal)
}
Attributed Title
The setTitle() method has no effect on a button's title if it's configured as "Attributed" in the Attributes inspector. To manage this situation, first get the attributed title from the button, then set the value.
#IBAction func button(sender: UIButton) {
let attributedTitle = sender.attributedTitle(for: .normal)
attributedTitle?.setValue("buttonName", forKey: "string")
sender.setAttributedTitle(attributedTitle, for: .normal)
}
In practically, if you use button from Storyboard or Xib file, and not clear button value in (Storyboard, Xib), than setTitle method doesn't work. If you cleared button value, setTitle method is work.
Create an IBOutlet for your button, then this code should work (assuming you named your IBOutlet button):
button.setTitle("title", forState: .Normal)
button.setTitle("title 1", forState: .Application)
button.setTitle("title 2", forState: .Highlighted)
button.setTitle("title 3", forState: .Reserved)
button.setTitle("title 4", forState: .Selected)
button.setTitle("title 5", forState: .Disabled)
place it in your viewDidLoad() method.
This worked for me.
dispatch_async(dispatch_get_main_queue(), ^{
[self.btnAssment setTitle:#"ASSDate" forState:UIControlStateNormal];
});
let button = UIButton()
button.setTitle("Button", for: .normal)
button.setTitleColor(.black, for: .normal)
I just had a similar issue, and none of the answer here worked in my case.
For me the solution was to call button.layoutSubviews() to update UIButton's label just after i changed the title.
(also I had to change the button type to custom, otherwise the title change would blink)

Hide UIButton title

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.