Help with iOS 15-style UIButton?
I am trying to construct a button using the new iOS15 button API. (Xcode 13ß3, UIKit & Storyboards)
The button has a title (aligned .leading) and an image (aligned .trailing). The button has the following layout constraints:
What I’m trying to accomplish is that the button should adjust its width based on the title content, not wrapping until the width causes the trailing constraint to be violated.
What actually happens is the the button’s text wraps. It acts as if it prefers wrapping to changing its size.
There are mentions in the documentation about disabling text wrapping for the button, but the obvious things, such as
button.titleLabel?.lineBreakMode = .byTruncatingMiddle
don’t work.
Has anyone whose tried working with iOS 15 buttons have any suggestions? |
|
Alex Zavatone
I did this in UIKit by making the button’s frame.size.width the frame.size.width of the content within + appropriate space buffers.
toggle quoted message
Show quoted text
One trick is if the text component is a UILabel, making the lines of text allowed to be 1 and the linebreak mode as you have. Have you sent the lines of text to 1? Let us know. Alex Zavatone
|
|
The problem is unique to the new iOS 15 UIButton API. There is no problem in my logic if the deployment target < iOS15.
Specifically,
|
|
Alex Zavatone
Do you have to use the new API? |
|
Alex Zavatone
|
|
Good thought. I'll give it a try.
Re: your "do you have to use the API" remark, the simple answer is that if your deployment target is iOS 15, you ARE using the API, whether you like it or not. There is no choice in this matter. And it's even worse -- if you just carry over code that works on iOS14 to an iOS15 deployment target, the button's appearance changes. In other words, iOS 15 will break existing code as soon as you link it for iOS 15 SDK deployment. This is seriously broken. |
|
Alex Zavatone
Thanks for the heads up. That’s crappy.
toggle quoted message
Show quoted text
|
|
Jeremy Hughes
Can you file a bug?
toggle quoted message
Show quoted text
Maybe they will fix it before iOS 15 is released. Jeremy — On 19 Jul 2021, at 16:06, Rick Aurbach via groups.io <rlaurb@...> wrote: |
|
I haven't filed a bug yet, but I will. In the meantime, I've constructed the following work around — it's a hack, but it seems to work.
toggle quoted message
Show quoted text
|
|
Alex Zavatone
Could you point out the specific workaround please?
toggle quoted message
Show quoted text
|
|
Alex Zavatone
And what is, "mirrow above bounds code”?
toggle quoted message
Show quoted text
|
|
Hi, Alex.
The work-around was to force the button width to be "expectedWidth" (which is the width of the [unwrapped] text + image padding + image width + 8 points of slop (to account for button margins). If you notice, I override draw() to always make this adjustment just before calling super.draw(). The 8 its of margin was determined by experiment. The comment about "mirroring above bounds code" just means that I was (historically) using different logic to adjust button size in the pre-15 case, and that I should probably change that code to use the same logic as the 15+ version. Hope this helps. |
|
Just to finish up this discussion, below is the code I ended up with for my button that is compatible with both pre-15 and post-15 code.
I think that if you subclass UIButton in your current code, (particularly if you use titleEdgeInsets and/or imageEdgeInsets, you need to test for breaking changes in iOS15. Good luck to all. |
|