Follow up to this thread, does adjustsFontSizeToFitWidth also follow the same thinking? Is it assumed that if we use a font style, this doesn't need to be set in a UIButton if we're using UIButtonConfiguration?
Post
Replies
Boosts
Views
Activity
Hello! This is a follow up to this thread but how do you set the number of lines in a UIButtonConfiguration? I'm assuming that maybe we can do this through using attributedTitle/NSAttributedString, but not sure which property or specific attribute to use. Can I get any help towards setting the amount of lines? Any help is appreciated!
When a UIButton configured with UIButtonConfiguration is selected and highlighted, my highlight colors are different in dark mode. Here's some relevant code
UIColor* kTitleColorStateNormal = [UIColor colorWithWhite:0.0 alpha:1.0];
UIColor* kTitleColorStateHighlighted = [UIColor colorWithWhite:0.0 alpha:0.3];
button.configurationUpdateHandler = ^(UIButton* incomingButton) {
UIButtonConfiguration* updatedConfig = incomingButton.configuration;
switch (incomingButton.state) {
case UIControlStateHighlighted:
updatedConfig.baseForegroundColor = kTitleColorStateHighlighted;
break;
case UIControlStateNormal:
updatedConfig.baseForegroundColor = kTitleColorStateNormal;
break;
default:
break;
}
incomingButton.configuration = updatedConfig;
};
This is the code used without UIButtonConfiguration
[button setTitleColor:kTitleColorStateNormal forState:UIControlStateNormal];
[button setTitleColor:kTitleColorStateHighlighted
forState:UIControlStateHighlighted];
and the output are these screenshots
Without UIButtonConfiguration
With UIButtonConfiguration
The white buttons are what the buttons look like in UIControlStateNormal. I agree that "with UIButtonConfiguration" does look better, but I would like the output to be the same, and it's not. Any help would be appreciated!
The default content inset is (top = 7, leading = 12, bottom = 7, trailing = 12), is there a reason this is the default rather than (0,0,0,0)? Is it to make it easier for people to use UIButtonConfiguration without having to fiddle with margin sizes to have elements look right? Thanks!
For the method -updateTraitsIfNeeded, if it's ran in traitCollectionDidChange is there a possibility for an infinite loop to trigger because we update the trait and then that triggers another traitCollectionDidChange? I know that the key words are "IfNeeded", but I wanted to make sure that this method forces an update usually once and then doesn't retrigger any traitCollection update logic recursively an infinite amount of time. Any help would be appreciated!
I made thread, but had a follow up question: What would be the equivalent refactor if we want to adjustsImageWhenHighlighted to be NO? I understood that we could manipulate the image to replicate adjusting the image when highlighted. Is there a way to replicate turning off any image adjustments when highlighted. What would the code look like?
The attached documentation for both of these properties
API_DEPRECATED("This property is ignored when using UIButtonConfiguration"
but what if we wanted to add spacing to the title or image? Just to confirm, is the equivalent refactor to use titlePadding and imagePadding in UIButtonConfiguration?
adjustsImageWhenHighlighted is deprecated and the header says this:
@property(nonatomic) BOOL adjustsImageWhenHighlighted API_DEPRECATED("This property is ignored when using UIButtonConfiguration, you may customize to replicate this behavior via a configurationUpdateHandler"
So far I have this
UIButtonConfiguration* buttonConfiguration =
[UIButtonConfiguration plainButtonConfiguration];
voice_search_button.configuration = buttonConfiguration;
voice_search_button.configurationUpdateHandler = ^(UIButton* button) {
};
but how do I make the image darker when highlighted/tapped? I should check if it's being highlighted and if it is then I add a tint to the UIButton? If so, what should that tint be to replicate the system color?
Thanks for the help!
In iOS17, traitCollectionDidChange gets triggered twice on iOS17 devices/simulators, while on iOS<17 devices/simulators, it only gets triggered once. I was wondering if there was a reason for this. I'm currently investigating issues between portrait and landscape and UI/view controllers looking weird because of the rotation and trying to figure out what the issue is. I found this thread https://developer.apple.com/forums/thread/732069 which makes me think that code should still work, but looking for any feedback that could help. Thanks!!
I can't seem to find the proper refactor for scaling down the title by using something like UIButton.titleLabel.minimumScaleFactor. Is there something in a NSAttributedString that I could use? If that's the case, I can probably use the attributedTitle property on UIButton.configuration. Let me know what you think!
I can't seem to find the equivalent. I don't think imagePadding is the right UIButtonConfiguration property. What would be the appropriate refactor the header comments just say this
// The effect of these properties can be replicated via UIButtonConfiguration.contentInset and UIButtonConfiguration.imageToTitlePadding. They are ignored when a configuration is set.
@property(nonatomic) UIEdgeInsets contentEdgeInsets API_DEPRECATED("This property is ignored when using UIButtonConfiguration", ios(2.0,15.0), tvos(2.0,15.0)) UI_APPEARANCE_SELECTOR; // default is UIEdgeInsetsZero. On tvOS 10 or later, default is nonzero except for custom buttons.
@property(nonatomic) UIEdgeInsets titleEdgeInsets API_DEPRECATED("This property is ignored when using UIButtonConfiguration", ios(2.0,15.0), tvos(2.0,15.0)); // default is UIEdgeInsetsZero
@property(nonatomic) UIEdgeInsets imageEdgeInsets API_DEPRECATED("This property is ignored when using UIButtonConfiguration", ios(2.0,15.0), tvos(2.0,15.0)); // default is UIEdgeInsetsZero
And I don't see imageToTitlePadding on Apple's public documentation so I'm not sure how to use it.
What is the replacement for UIButton.titleLabel.adjustsFontForContentSizeCategory in UIButtonConfiguration? If I want to replace UIButton.titleLabel in general, I need to create an NSAttributedString and then manipulate it, but I'm not sure if there's a NSAttributeKey for this property. Any help is appreciated! Feel free to add any other refactors that seem relevant!
Hello, I'm having an issue with UIButtonConfiguration not respecting the constraints of the button. I'm trying to replace a UIButton that contains a titleLabel, but the titleLabel doesn't get transferred over. So I tried setting an attributedTitle to UIButtonConfiguration, and that made the button show, but the constraints were messed up and the button was large. The constraints of the UIView created by UIButtonConfiguration overrode the UIView created from the UIButton. Is there any way to abide by the deprecated API that asks us to use UIButtonConfiguration but respect the other properties of UIButton. It doesn't feel like UIButtonConfiguration is ready for all the functionality needed in a custom UIButton.