Skip to content

Commit f6ec10b

Browse files
authored
[iOS] Add a Container around ImageButton (#24020)
* Add a Container around ImageButton * Update PublicAPI.Unshipped.txt * - don't animate push
1 parent 7cb2341 commit f6ec10b

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
namespace Maui.Controls.Sample.Issues
2+
{
3+
[Issue(IssueTracker.Github, 23897, "Add a permanent wrapper around ImageButton so it works better with loading and unloading", PlatformAffected.iOS)]
4+
public class Issue23897 : NavigationPage
5+
{
6+
7+
public Issue23897() : base(new TestPage())
8+
{
9+
}
10+
11+
public class TestPage : ContentPage
12+
{
13+
Label _labelLoadedCount;
14+
Label _labelUnloadedCount;
15+
ImageButton _imageButton;
16+
private int _loadedCount;
17+
private int _unloadedCount;
18+
19+
public TestPage()
20+
{
21+
_imageButton = new ImageButton()
22+
{
23+
Source = "coffee.png",
24+
AutomationId = "ImageButton",
25+
HeightRequest = 100,
26+
WidthRequest = 100
27+
};
28+
29+
_imageButton.Loaded += (s, e) =>
30+
{
31+
_labelLoadedCount.Text = $"{++_loadedCount}";
32+
};
33+
34+
_imageButton.Unloaded += (s, e) =>
35+
{
36+
_labelUnloadedCount.Text = $"{++_unloadedCount}";
37+
};
38+
39+
_labelUnloadedCount = new Label()
40+
{
41+
AutomationId = "UnloadedCount",
42+
Text = "0"
43+
};
44+
45+
_labelLoadedCount = new Label()
46+
{
47+
AutomationId = "LoadedCount",
48+
Text = "0"
49+
};
50+
51+
Content = new VerticalStackLayout()
52+
{
53+
new Label()
54+
{
55+
Text = "ImageButton Loaded Count:",
56+
},
57+
_labelLoadedCount,
58+
new Label()
59+
{
60+
Text = "ImageButton Unloaded Count:",
61+
},
62+
_labelUnloadedCount,
63+
_imageButton,
64+
new Button()
65+
{
66+
Text = "Push And Pop Page to Validate ImageButton Loading/Unloaded",
67+
AutomationId = "PushAndPopPage",
68+
LineBreakMode = LineBreakMode.WordWrap,
69+
Command = new Command(async () =>
70+
{
71+
await Navigation.PushAsync(new ContentPage(), false);
72+
await Task.Yield();
73+
await Navigation.PopAsync(true);
74+
})
75+
}
76+
};
77+
}
78+
}
79+
}
80+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues
6+
{
7+
public class Issue23897(TestDevice device) : _IssuesUITest(device)
8+
{
9+
public override string Issue => "Add a permanent wrapper around ImageButton so it works better with loading and unloading";
10+
11+
[Test]
12+
[Category(UITestCategories.ImageButton)]
13+
public void LoadingAndUnloadingWorksForImageButton()
14+
{
15+
for (int i = 0; i < 3; i++)
16+
{
17+
var loadedCount = App.WaitForElement("LoadedCount").GetText();
18+
var unLoadedCount = App.WaitForElement("UnloadedCount").GetText();
19+
Assert.That(loadedCount, Is.EqualTo($"{i + 1}"));
20+
Assert.That(unLoadedCount, Is.EqualTo($"{i}"));
21+
22+
App.Tap("PushAndPopPage");
23+
}
24+
}
25+
}
26+
}

src/Core/src/Handlers/ImageButton/ImageButtonHandler.iOS.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace Microsoft.Maui.Handlers
55
{
66
public partial class ImageButtonHandler : ViewHandler<IImageButton, UIButton>
77
{
8+
// Because we can't inherit from Button we use the container to handle
9+
// Life cycle events and things like monitoring focus changed
10+
public override bool NeedsContainer => true;
11+
812
readonly ImageButtonProxy _proxy = new();
913

1014
protected override UIButton CreatePlatformView()

src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Microsoft.Maui.Platform.MauiScrollView.MauiScrollView() -> void
5050
Microsoft.Maui.SoftInputExtensions
5151
override Microsoft.Maui.Handlers.ButtonHandler.NeedsContainer.get -> bool
5252
override Microsoft.Maui.Handlers.ContentViewHandler.DisconnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void
53+
override Microsoft.Maui.Handlers.ImageButtonHandler.NeedsContainer.get -> bool
5354
override Microsoft.Maui.Handlers.RefreshViewHandler.SetVirtualView(Microsoft.Maui.IView! view) -> void
5455
override Microsoft.Maui.Handlers.ScrollViewHandler.NeedsContainer.get -> bool
5556
override Microsoft.Maui.Handlers.SwipeItemButton.Frame.get -> CoreGraphics.CGRect

src/Core/src/PublicAPI/net-maccatalyst/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Microsoft.Maui.SoftInputExtensions
5353
*REMOVED*override Microsoft.Maui.Handlers.BorderHandler.PlatformArrange(Microsoft.Maui.Graphics.Rect rect) -> void
5454
override Microsoft.Maui.Handlers.ButtonHandler.NeedsContainer.get -> bool
5555
override Microsoft.Maui.Handlers.ContentViewHandler.DisconnectHandler(Microsoft.Maui.Platform.ContentView! platformView) -> void
56+
override Microsoft.Maui.Handlers.ImageButtonHandler.NeedsContainer.get -> bool
5657
override Microsoft.Maui.Handlers.RefreshViewHandler.SetVirtualView(Microsoft.Maui.IView! view) -> void
5758
override Microsoft.Maui.Handlers.ScrollViewHandler.NeedsContainer.get -> bool
5859
override Microsoft.Maui.Handlers.SwipeItemButton.Frame.get -> CoreGraphics.CGRect

0 commit comments

Comments
 (0)