Skip to content

Commit 6e75f3e

Browse files
kubaflormarinho
andauthored
[iOS] Auto Resize chrome icons on iOS to make it more consistent with other platforms - Toolbar items (#30995)
* [iOS] Auto Resize chrome icons - Toolbar items * [testing] Update test screenshots * Update ToolbarItemExtensions.cs * Updated toolbar item rendering snapshots --------- Co-authored-by: Rui Marinho <me@ruimarinho.net>
1 parent 693cdfe commit 6e75f3e

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/Controls/src/Core/Compatibility/iOS/Extensions/ToolbarItemExtensions.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CoreGraphics;
55
using Foundation;
66
using Microsoft.Maui.Graphics;
7+
using Microsoft.Maui.Graphics.Platform;
78
using Microsoft.Maui.Platform;
89
using ObjCRuntime;
910
using UIKit;
@@ -57,6 +58,35 @@ internal static SecondarySubToolbarItem ToSecondarySubToolbarItem(this ToolbarIt
5758
return new SecondarySubToolbarItem(item, action);
5859
}
5960

61+
static UIImage ScaleImageToSystemDefaults(ImageSource imageSource, UIImage uIImage)
62+
{
63+
var icon = uIImage;
64+
65+
var originalImageSize = icon?.Size ?? CGSize.Empty;
66+
67+
// The largest height you can use for navigation bar icons in iOS.
68+
// Per Apple's Human Interface Guidelines, the navigation bar height is 44 points,
69+
// so using the full height ensures maximum visual clarity and maintains consistency
70+
// with iOS design standards. This allows icons to utilize the entire available
71+
// vertical space within the navigation bar container.
72+
var defaultIconHeight = 44f;
73+
var buffer = 0.1;
74+
// We only check height because the navigation bar constrains vertical space (44pt height),
75+
// but allows horizontal flexibility. Width can vary based on icon design and content,
76+
// while height must fit within the fixed navigation bar bounds to avoid clipping.
77+
78+
// if the image is bigger than the default available size, resize it
79+
if (icon is not null && originalImageSize.Height - defaultIconHeight > buffer)
80+
{
81+
if (imageSource is not FontImageSource fontImageSource || !fontImageSource.IsSet(FontImageSource.SizeProperty))
82+
{
83+
icon = icon.ResizeImageSource(originalImageSize.Width, defaultIconHeight, originalImageSize);
84+
}
85+
}
86+
87+
return icon;
88+
}
89+
6090
sealed class PrimaryToolbarItem : UIBarButtonItem
6191
{
6292
readonly bool _forceName;
@@ -302,11 +332,11 @@ void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
302332

303333
void UpdateIcon(ToolbarItem item)
304334
{
305-
if (item.IconImageSource != null && !item.IconImageSource.IsEmpty)
335+
if (item.IconImageSource is not null && !item.IconImageSource.IsEmpty)
306336
{
307337
item.IconImageSource.LoadImage(item.FindMauiContext(), result =>
308338
{
309-
((SecondaryToolbarItemContent)CustomView).Image = result?.Value;
339+
((SecondaryToolbarItemContent)CustomView).Image = ScaleImageToSystemDefaults(item.IconImageSource, result?.Value);
310340
});
311341
}
312342
else
3.77 KB
Loading
16.9 KB
Loading

0 commit comments

Comments
 (0)