UIButtonConfiguration Help

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.

Answered by Frameworks Engineer in 749845022

Given what you linked to, if you want to migrate to UIButtonConfiguration you likely want to use attributed strings instead of trying to modify the button label's font (when using the button configuration path we will always set an attributed title, which will cause the font you specify to be ignored). Basically your adoption of button configuration just for the content insets isn't really enough to get what you want from button configuration – your better off sticking with the deprecated API until you can more fully migrate to the new API.

Without the code your using to setup the button its hard to understand what your running into...

Here's a link to the code I'm trying to work with https://source.chromium.org/chromium/chromium/src/+/main:ios/chrome/browser/ui/price_notifications/cells/price_notifications_track_button.mm;l=26-61?q=price_notifications_track_button. Specifically, I'm trying to deprecate the legacy UIButton system and replace it with UIButtonConfiguration.

Accepted Answer

Given what you linked to, if you want to migrate to UIButtonConfiguration you likely want to use attributed strings instead of trying to modify the button label's font (when using the button configuration path we will always set an attributed title, which will cause the font you specify to be ignored). Basically your adoption of button configuration just for the content insets isn't really enough to get what you want from button configuration – your better off sticking with the deprecated API until you can more fully migrate to the new API.

This issue isn't resolved, I just hit the solve button by accident and can't remove it

Can we possibly remove the deprecation warning since UIButtonConfiguration doesn't have the full functionality required to migrate over from the legacy UIButton system? LineBreakMode wasn't even added until recently in iOS 16.

Even in this example of a sample project

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  [self.view setBackgroundColor:[UIColor whiteColor]];
  UIButton *legacyButton = [[UIButton alloc] init];
  [self updateButton:legacyButton withFloat:210];
  [self.view addSubview:legacyButton];
  
  UIButtonConfiguration* buttonConfiguration = [UIButtonConfiguration plainButtonConfiguration];
  UIButton *configButton = [UIButton buttonWithConfiguration:buttonConfiguration primaryAction:nil];
  [self updateButton:configButton withFloat:260];
  [self.view addSubview:configButton];
  
  UIButton *mixedButton = [[UIButton alloc] init];
  [self updateButton:mixedButton withFloat:310];
  mixedButton.configuration = [UIButtonConfiguration plainButtonConfiguration];
  [self.view addSubview:mixedButton];
  
}

- (void) updateButton:(UIButton*)button withFloat: (CGFloat)yAxis {
  button.titleLabel.font =
      [[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]
          fontWithSize:17.0];
  [button setBackgroundColor:[UIColor blueColor]];
  [button setTitle:@"Legacy" forState:UIControlStateNormal];
  button.frame = CGRectMake(80.0, yAxis, 160.0, 40.0);
}
@end

You will notice that the UILabel doesn't respect the boundaries of the UIButton. Please help

Also I'm having an issue with this code

  UIButton *mixedButton = [UIButton buttonWithType:UIButtonTypeSystem];
  [self updateButton:mixedButton withFloat:420];
  mixedButton.configuration = [UIButtonConfiguration plainButtonConfiguration];
  [mixedButton setImage:[UIImage imageNamed:@"Tweet"] forState:UIControlStateNormal];
  mixedButton.tintColor = [UIColor redColor];
  [self.view addSubview:mixedButton];

How come the tint color doesn't affect the image set when there's a configuration set? From what I read, old methods and properties should override configuration changes.

UIButtonConfiguration Help
 
 
Q