uibutton.setTitle change text without changing font size - ios

First, I added a button and set it's size to be 40
Then, in ViewController, I will change its text when user tap the button
startStopBtn.setTitle("START", for: .normal)
But not only the title change, the size also changed to 17.
Why is that? I want to keep the size unchanged.
Im using swift 5

iOS 15 introduced Configuration settings for many controls -- including UIButton.
When you create a button using Storyboard / Interface Builder in Xcode 13, the default configuration is Plain. Without changing that, you need to use the options via UIButton.Configuration to change much (most) of the button's appearance.
Selecting Default from the Button Style drop-down gives you a "pre-iOS 15" type of button.

Related

Codename One - addSubmitButtons method of Validator class changes button style

I'm developing a Codename One app for iOS.
I defined a button as follows:
Button signUpButton = new Button();
signUpButton.getAllStyles().setBorder(Border.createEmpty());
signUpButton.getAllStyles().setBgTransparency(255);
signUpButton.getAllStyles().setBgColor(0x9d1710);
signUpButton.getAllStyles().setFgColor(0x848484);
signUpButton.getAllStyles().setMargin(Component.LEFT, 4);
signUpButton.getAllStyles().setMargin(Component.RIGHT, 4);
signUpButton.setCapsText(true);
signUpButton.setText("Conferma");
After that, I created a Validator, I added some constraits to other text fields and then I added the button to the validator:
validator.addSubmitButtons(signUpButton);
The addSubmitButtons method changes the style I set for the button (in particular, the margins and size changed).
I also tried to set the style I want to the button after the addSubmitButtons method, but it seems it doesn't consider it. It happens on simulator and on device too.
This is what happens without calling the addSubmitButtons method:
This is what happens calling the addSubmitButtons method:
How can I solve that problem?
When the validation doesn't pass the button is set to disabled so the the disabled style is picked of the 4 styles.
I'm guessing here but I'm assuming that the margin/padding of the disabled style is still set in pixels instead of millimeters for the native theme. If you explicitly set all styles to use millimeters it should work.

UIButton Image Not Changing With Interface Builder Settings (Selected & Disabled)

I am trying to set the toggle state of a UIButton through the interface builder. I have done it successfully using code, but I need to get it working through the interface builder.
Im not sure what the issue is but for selected I set its image to 'flashOn' and for disabled I set it to 'flashOff'
When I hold down the image, it shows the 2nd image, but pressing the image does not toggle between these two images.
Im certain it's super simple so if you can suggest what I am missing I would appreciate it
A button's selected and disabled states need to be set in code.
Can you show your implementation for when you got desired results through code? I think, from what I can piece together from your question, that you have mixed up the states of the button.
Default - images will display for this button in any state unless another is specified explicitely
Disabled - image will display when button is disabled. (button will not switch from this state through user interaction as it is disabled)
Selected - image will display when the button is selected, which as far as I know is only when you set the button as selected through code.
Highlighted - image will display when you press and hold down on the button

iOS get UINavigationBar's back button in order to set accessibility properties

I have a navigation controller stack where one of the views has a dynamic title.
The view controllers and their titles go like this:
Main --> ItemsTableView --> ItemDetails
Title:Main Title: NN Items Title: Details
Because the iOS UINavigationController sets the text of the "Back" button to be the title of the previous screen, the "Back" button on the details screen says "< NN Items" where NN is a dynamically changing number.
I'm trying to do some iOS UI automation, but the accessibility Label / ID of the back button is set by the system to it's button text. This means that the accessibility label of the back button on the details screen will change dynamically, and I can't find it from my scripts!
If I could get a reference to the UIBarButtonItem then I could easily set it's accessibilityLabel or accessibilityIdentifier from code to be a fixed string, however I can't figure out how to do this?
All of the stuff I've been able to find references setting the back button to a custom button via self.navigationItem.backBarButtonItem or similar, but when I read this property it's nil. I haven't been able to find out how to get access to the standard item without replacing it. I'd prefer not to replace the button if possible
This was bugging me as well. I've been writing Xcode 7 UI Tests and was trying to come up with a generic way of tapping on the back button without having to replace it with a custom button.
The following is how I solved this for Xcode 7 UI Tests - but you may also be able to apply this to UI Automation as well.
I discovered that (in terms of Xcode 7 UI Tests at least) the back bar button item that is created by the system consists of two buttons the entire thing is a button with an accessibility label of whatever the title of the button is, and then the arrow is also a button with an accessibility label of "Back".
Thus, as long as there aren't any other buttons on the screen that are identified as "Back", the back button can be accessed via the accessibility label of "Back". Like so in the case of UI Tests:
[[app.buttons matchingIdentifier:#"Back"] elementBoundByIndex:0]
Here I'm getting the first button that can be identified by "Back". I my case there could only ever be two such buttons - the arrow, or the whole back button itself (in the case where the back button's title is also "Back"). Since both of these buttons are essentially the same, just getting the first one it finds is sufficient.

How do you set the selected image for a button in Xcode?

I know how to change the image programatically when a button is pressed, but there must be a way to do this non programatically in Xcode, but despite searching high and low I simply cannot see anywhere in Xcode where the selected image can be specified.
Try in your storyboard. The second selection (STATE CONFIG) down in attributes inspector should show "default" for your button. Change the sate (Highlighted, selected, disabled) and then change the image in the image box 8 down or background image underneath.
Open up the "Attributes Inspector" in your storyboard or XIB and the image name is an attribute you can set in your UIButton object.
Also make certain to make the "Title" empty or a string will be drawn over your image.

Does this UIBarButton exists by default in iOS? How is it called

I think I've seen this kind of button before, and now I need one.
A button like the one below who's label can be changed to whatever number I need (from 2 to 10 for example).
I have a feeling that this exists by default in iOS SDK but I can't seem to find it.
Does it exist by default or do I have to create it myself?
Thanks!
Its easy to replicate. Add a new RoundRectButton in interface builder, then in the attributes inspector change the type to Custom. Set the BackgroundImage to an image similar to above, and then set the Title to the number. By default the title will centered in the button, but you can use the Inset options in the attributes inspector and nudge the title in and down using the Top and Left inset attributes.
No, it's not a default item as far as I know. You can see all the default items in the Apple Human Interface Guidelines (see "System-Provided Buttons and Icons").

Resources