Is there a way to setTitle on a UIButton that updates all UIControlStates at once?

uibutton settitle not working swift
uibutton titlelabel
uibutton settitlecolor not working
uibutton get title
uibutton not showing
uibutton title not showing swift
uicontrolstatenormal
uibutton change text color when pressed

I have a UIButton, and I'd like to update its title, but I'd rather not have to always do it for each and every state like the following:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

[myButton setTitle:@"Play" forState:UIControlStateHighlighted];

[myButton setTitle:@"Play" forState:UIControlStateSelected];

Is there a better way?

According to the documentation you should only need to call:

In Objective-C:

[myButton setTitle:@"Play" forState:UIControlStateNormal];

In Swift:

myButton.setTitle("Play", for: .normal)

The UIButton docs explain why:

In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value. If the value for UIControlStateNormal is not set, then the property defaults to a system value. Therefore, at a minimum, you should set the value for the normal state.

Namely, that if you only set the normal value the other states will refer to it when being set.

setTitle:forState:, (void)setTitle:(NSString *)title forState:(UIControlState)state; and an attributed title for the button, the button prefers the use of the attributed title over this one. Use this method to set the title for the button. The title you specify derives its formatting from the button’s associated label object. If you set both a title and an attributed title for the button, the button prefers the use of the attributed title over this one. At a minimum, you should set the value for the normal state.

or you can setTitle by :

[myButton setTitle:@"Play" forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];

iOS Development with Xamarin Cookbook, The button is initialized with the UIButton.FromType(UIButtonType) static method. This method SetTitle ("Tap to change view color", UIControlState.Normal);  UIButton: UIControl intercepts touch events and sends an action message to a target object when it's tapped. You can set the title, image, and other appearance properties of a button. You can set the title, image, and other appearance properties of a button.

You can create a category for UIButton:

@implementation UIButton (Addition)
-(void)setTitleForAllStates:(NSString *)title {
     //you can add/remove this area : UIControlStateApplication, UIControlStateDisabled, UIControlStateReserved
     [self setTitle:title forState:UIControlStateNormal];
     [self setTitle:title forState:UIControlStateSelected];
     [self setTitle:title forState:UIControlStateHighlighted];
}
@end

IOS Components and Frameworks: Understanding the Advanced Features , For example, one project uses method swizzling to replace the setTitle: As a contrived example, consider swizzling setTitle:forState: on UIButton. The swizzled method will automatically update all the button and overriding that method, but this example shows how the same result can be achieved with method swizzling. fbrereto. Programmer . 537. 62 Is there a way to setTitle on a UIButton that updates all UIControlStates at once? Sep 3 '09.

2019

It's just

yourButton.setTitle("Click me", for: .normal)

IOS Development Using MonoTouch Cookbook: 109 Simple But , FromType(UIButtonType). This method takes one parameter of the type UIButtonType and returns SetTitle ("Tap to changeview color", UIControlState.​Normal);  47 Is there a way I can capture my iPhone screen as a video? [closed] Oct 14 '08 31 Is there a way to setTitle on a UIButton that updates all UIControlStates at once?

The answer:

button.setTitle("All", for: .normal)

or

button.setTitle("All", for: [])

is not correct in general, because it works only if the title for some state is not already set. For example:

button.setTitle("N", for: .normal)
button.setTitle("HL", for: .highlighted)
button.setTitle("All", for: .normal)

After this code button still have title "HL" for highlighted state. So to change title for all used states in general case, you have to loop through all these states:

let states: [UIControl.State] = [.normal, .highlighted, .selected, [.highlighted, .selected]]
for state in states {
  button.setTitle("All", for: state)
}

(If you use other states e.g. .disabled, you have to add they combinations to loop as well)

Note: UIControl.State is a Set of options, so setting the title with:

button.setTitle("All", for: [.selected, .highlighted])

does not set title "All" for both selected and highlighted states, instead it sets the title "All" for the combined state which is selected and highlighted at the same time.

iOS 5 Recipes: A Problem-Solution Approach, A label with no shadow compared with a one square point shadow The As the cornerstone of two-way user interaction, the UIButton allows you to actively For this reason, most of the UIButton methods include a parameter for a UIControlState. than accessing the titleLabel property, you should use the -​setTitle:forState:  Nobody loves UIButton Dating back all the way to the first iOS SDK you’d think there’d be a bit more love for this venerable component. I’m rather particular about ensuring my apps follow accessibility best practices.

Program the Internet of Things with Swift for iOS, In Listing 12-8 we can see the entire ViewController.swift code that we will use var clearButton : UIButton! setTitle("Blink All Lights", forState: UIControlState. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

setAttributedTitle:forState:, The possible values are described in UIControlState . If you set both a title and an attributed title for the button, the button prefers the use of setTitle:forState:. fbrereto. Programmer . 9. answers. 70. 62 Is there a way to setTitle on a UIButton that updates all UIControlStates at once? 27 Is there a way to automate

UIButton - Update button label when pressed, UIButton - Update button label when pressed. This post shows how to change the text/title of a button every time the button is pressed. The code below is written in button.setTitle("Press", for: UIControlState.normal). button. All one can think and do in a short time is to think what one already knows and to do as one has always done! iOS create UIButton programmatically Button is an important element of any screen design.

Comments
  • That's not what I experience
  • This is also not true for buttons of type .custom.
  • Why would you want to do it this way?
  • to setup title of multiple state button in one line.
  • That's the exact way for all controls. UI Slider does not change in all states. helpful
  • That is not working because state is NS_OPTIONS. If you use UIControlStateNormal|UIControlStateHighlighted, you can not get title visible when button shows.