@@ -182,19 +182,13 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt
182182
183183 platformButton . UpdatePadding ( button ) ;
184184
185- // #pragma warning disable CA1416, CA1422 // TODO: [UnsupportedOSPlatform("ios15.0")]
186- // if (platformButton.ImageEdgeInsets != imageInsets ||
187- // platformButton.TitleEdgeInsets != titleInsets)
188- // {
189- // platformButton.ImageEdgeInsets = imageInsets;
190- // platformButton.TitleEdgeInsets = titleInsets;
191- // platformButton.Superview?.SetNeedsLayout();
192- // }
193- // #pragma warning restore CA1416, CA1422
194185
186+ // On iOS 15+, we will resize the image and then use the UIButton.Configuration to adjust the padding
195187 if ( OperatingSystem . IsIOSVersionAtLeast ( 15 ) && platformButton . Configuration is not null )
196188 {
197- var config = platformButton . Configuration ;
189+ ResizeImage ( platformButton , button , image ) ;
190+ var config = platformButton . Configuration ;
191+
198192 config . ImagePadding = imageInsets . Right - imageInsets . Left + titleInsets . Left - titleInsets . Right ;
199193 platformButton . Configuration = config ;
200194 }
@@ -211,6 +205,61 @@ public static void UpdateContentLayout(this UIButton platformButton, Button butt
211205 }
212206 }
213207
208+ static void ResizeImage ( UIButton platformButton , Button button , UIImage image )
209+ {
210+ nfloat buttonSize = 0 ;
211+
212+ if ( ! OperatingSystem . IsIOSVersionAtLeast ( 15 ) )
213+ {
214+ var contentEdgeInsets = platformButton . ContentEdgeInsets ;
215+ if ( platformButton . Bounds != CGRect . Empty )
216+ {
217+ if ( platformButton . Bounds . Height > platformButton . Bounds . Width )
218+ buttonSize = platformButton . Bounds . Width - ( contentEdgeInsets . Left + contentEdgeInsets . Right ) ;
219+ else
220+ buttonSize = platformButton . Bounds . Height - ( contentEdgeInsets . Top + contentEdgeInsets . Bottom ) ;
221+ }
222+ }
223+ else
224+ {
225+ if ( platformButton . Bounds != CGRect . Empty && platformButton . Configuration ? . ContentInsets is NSDirectionalEdgeInsets contentInsets )
226+ {
227+ if ( platformButton . Bounds . Height > platformButton . Bounds . Width )
228+ buttonSize = platformButton . Bounds . Width - ( contentInsets . Leading + contentInsets . Trailing ) ;
229+ else
230+ buttonSize = platformButton . Bounds . Height - ( contentInsets . Top + contentInsets . Bottom ) ;
231+ }
232+ }
233+
234+ try
235+ {
236+ if ( buttonSize != 0 )
237+ image = ResizeImageSource ( image , buttonSize , buttonSize ) ;
238+
239+ image = image ? . ImageWithRenderingMode ( UIImageRenderingMode . AlwaysOriginal ) ;
240+
241+ platformButton . SetImage ( image , UIControlState . Normal ) ;
242+ }
243+ catch ( Exception )
244+ {
245+ // Handler.MauiContext?.CreateLogger<ButtonHandler>()?.LogWarning("Can not load Button ImageSource");
246+ }
247+ }
248+
249+ static UIImage ResizeImageSource ( UIImage sourceImage , nfloat maxWidth , nfloat maxHeight )
250+ {
251+ if ( sourceImage is null || sourceImage . CGImage is null )
252+ return null ;
253+
254+ var sourceSize = sourceImage . Size ;
255+ float maxResizeFactor = ( float ) Math . Min ( maxWidth / sourceSize . Width , maxHeight / sourceSize . Height ) ;
256+
257+ if ( maxResizeFactor > 1 )
258+ return sourceImage ;
259+
260+ return UIImage . FromImage ( sourceImage . CGImage , sourceImage . CurrentScale / maxResizeFactor , sourceImage . Orientation ) ;
261+ }
262+
214263 public static void UpdateText ( this UIButton platformButton , Button button )
215264 {
216265 var text = TextTransformUtilites . GetTransformedText ( button . Text , button . TextTransform ) ;
0 commit comments