Skip to content

Commit ec6ee45

Browse files
kubafloPureWeen
authored andcommitted
[iOS] SwipeView Closes when Content Changes - fix (#29088)
* [iOS] SwipeView Closes when Content Changes - fix * Added snapshots
1 parent aabad09 commit ec6ee45

File tree

6 files changed

+125
-2
lines changed

6 files changed

+125
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Name="Self"
5+
x:Class="Maui.Controls.Sample.Issues.Issue29086">
6+
<CollectionView ItemsSource="{Binding Numbers}"
7+
Margin="60">
8+
<CollectionView.ItemTemplate>
9+
<DataTemplate>
10+
<SwipeView>
11+
<SwipeView.LeftItems>
12+
<SwipeItems
13+
Mode="Reveal"
14+
SwipeBehaviorOnInvoked="RemainOpen">
15+
<SwipeItemView>
16+
<Button
17+
Text="+"
18+
AutomationId="AddButton"
19+
WidthRequest="40"
20+
BackgroundColor="LightGreen"
21+
Command="{Binding Source={x:Reference Self}, Path=IncrementCommand}"
22+
CommandParameter="{Binding .}"/>
23+
</SwipeItemView>
24+
<SwipeItemView>
25+
<Button
26+
Text="-"
27+
WidthRequest="40"
28+
BackgroundColor="LightCoral"
29+
Command="{Binding Source={x:Reference Self}, Path=DecrementCommand}"
30+
CommandParameter="{Binding .}"/>
31+
</SwipeItemView>
32+
</SwipeItems>
33+
</SwipeView.LeftItems>
34+
35+
<Grid Padding="10"
36+
AutomationId="SwipeItem"
37+
BackgroundColor="LightBlue">
38+
<Label Text="{Binding Value}"
39+
FontSize="18"
40+
HorizontalOptions="Center"/>
41+
</Grid>
42+
</SwipeView>
43+
</DataTemplate>
44+
</CollectionView.ItemTemplate>
45+
</CollectionView>
46+
</ContentPage>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.ObjectModel;
2+
using System.Windows.Input;
3+
4+
namespace Maui.Controls.Sample.Issues;
5+
6+
[Issue(IssueTracker.Github, 29086, "SwipeView Closes when Content Changes even with SwipeBehaviorOnInvoked='RemainOpen'", PlatformAffected.iOS)]
7+
public partial class Issue29086 : ContentPage
8+
{
9+
public ObservableCollection<NumberItem> Numbers { get; set; }
10+
public ICommand IncrementCommand { get; }
11+
public ICommand DecrementCommand { get; }
12+
13+
public Issue29086()
14+
{
15+
InitializeComponent();
16+
17+
Numbers = new ObservableCollection<NumberItem>
18+
{
19+
new NumberItem { Value = 1 },
20+
};
21+
22+
IncrementCommand = new Command<NumberItem>((item) => item.Value++);
23+
DecrementCommand = new Command<NumberItem>((item) => item.Value--);
24+
25+
BindingContext = this;
26+
}
27+
28+
public class NumberItem : ViewModelBase
29+
{
30+
int _value;
31+
public int Value
32+
{
33+
get => _value;
34+
set { _value = value; OnPropertyChanged(); }
35+
}
36+
}
37+
}
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 Issue29086 : _IssuesUITest
8+
{
9+
10+
public Issue29086(TestDevice testDevice) : base(testDevice)
11+
{
12+
}
13+
14+
public override string Issue => "SwipeView Closes when Content Changes even with SwipeBehaviorOnInvoked='RemainOpen'";
15+
16+
[Test]
17+
[Category(UITestCategories.SwipeView)]
18+
public void SwipeViewShouldNotClose()
19+
{
20+
App.WaitForElement("SwipeItem");
21+
App.SwipeLeftToRight("SwipeItem");
22+
App.Click("AddButton");
23+
App.Click("AddButton");
24+
VerifyScreenshot();
25+
}
26+
}
Loading
Loading

src/Core/src/Platform/iOS/MauiSwipeView.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public override void LayoutSubviews()
8080

8181
if (_contentView != null && _contentView.Frame.IsEmpty)
8282
_contentView.Frame = Bounds;
83+
84+
if (_isOpen)
85+
{
86+
Swipe(animated: false);
87+
}
8388
}
8489

8590
public override void TouchesEnded(NSSet touches, UIEvent? evt)
@@ -581,7 +586,7 @@ void Swipe(bool animated = false)
581586

582587
if (_swipeTransitionMode == SwipeTransitionMode.Reveal)
583588
{
584-
Animate(swipeAnimationDuration, 0.0, UIViewAnimationOptions.CurveEaseOut, () =>
589+
void SetFrame()
585590
{
586591
switch (_swipeDirection)
587592
{
@@ -598,7 +603,16 @@ void Swipe(bool animated = false)
598603
_contentView.Frame = new CGRect(_originalBounds.X, _originalBounds.Y + offset, _originalBounds.Width, _originalBounds.Height);
599604
break;
600605
}
601-
}, null);
606+
}
607+
608+
if (animated)
609+
{
610+
Animate(swipeAnimationDuration, 0.0, UIViewAnimationOptions.CurveEaseOut, SetFrame, null);
611+
}
612+
else
613+
{
614+
SetFrame();
615+
}
602616
}
603617

604618
if (_swipeTransitionMode == SwipeTransitionMode.Drag)

0 commit comments

Comments
 (0)