Align UIAlertAction Title to the Left

2019-08-18 22:24发布

问题:

I've been trying to align the title of an UIAlertAction item, but I can't seem to make it work.

Do you guys have any idea how to make it work in Objective-C?

回答1:

[alertAction setValue:[NSNumber numberWithInt:NSTextAlignmentLeft]
forKey:@"titleTextAlignment"];


回答2:

UIAlertController is an opaque class. That kind of customization isn't supported. If you want, you can try to roll your own, or find an open source alternative.

I've tried hacking it by traversing the view hierarchy and finding labels, but it didn't seem to work. I also tested with UIAppearance, but labels don't respond to alignment changes.

My sample code (Swift) is on GitHub.

Some open source alternatives:

  • DAAlertController
  • PMAlertController
  • EZAlertController