Skip to content

Commit 72eeb4c

Browse files
committed
Fixes #18204 border lagging behind content on iOS and improves initial render performance
1 parent 4fabb3e commit 72eeb4c

5 files changed

Lines changed: 58 additions & 6 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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:Class="Maui.Controls.Sample.Issues.Issue18204"
5+
Title="Issue18204">
6+
7+
<VerticalStackLayout Padding="24">
8+
<Border
9+
HorizontalOptions="Center"
10+
x:Name="TheBorder"
11+
BackgroundColor="LightBlue"
12+
Shadow="{Shadow Brush=Black, Offset='0,2', Radius=2, Opacity=0.20}"
13+
StrokeShape="{RoundRectangle CornerRadius=20}">
14+
<Button x:Name="TheButton" Clicked="ButtonClicked" BackgroundColor="LightGreen" WidthRequest="200" HeightRequest="500" />
15+
</Border>
16+
</VerticalStackLayout>
17+
</ContentPage>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using Microsoft.Maui;
3+
using Microsoft.Maui.Controls;
4+
using Microsoft.Maui.Controls.Xaml;
5+
6+
namespace Maui.Controls.Sample.Issues;
7+
8+
[XamlCompilation(XamlCompilationOptions.Compile)]
9+
[Issue(IssueTracker.Github, 18204, "[iOS] Drawing of Borders lags behind other elements creating bizarre overlaps and glitches", PlatformAffected.iOS)]
10+
11+
public partial class Issue18204 : ContentPage
12+
{
13+
public Issue18204()
14+
{
15+
InitializeComponent();
16+
}
17+
18+
private void ButtonClicked(object sender, EventArgs e)
19+
{
20+
var button = (Button)sender;
21+
button.CancelAnimations();
22+
var targetHeight = button.HeightRequest == 200.0 ? 500.0 : 200.0;
23+
button.Animate("Height", new Animation(v => button.HeightRequest = v, button.Height, targetHeight, Easing.Linear));
24+
}
25+
}

src/Core/src/Handlers/Border/BorderHandler.iOS.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,7 @@ static partial void UpdateContent(IBorderHandler handler)
6363

6464
public override void PlatformArrange(Rect rect)
6565
{
66-
// Disable the animation during arrange for the Border; otherwise, all resizing actions
67-
// will animate, and it makes the Border lag behind its content.
68-
69-
CATransaction.Begin();
70-
CATransaction.AnimationDuration = 0;
7166
base.PlatformArrange(rect);
72-
CATransaction.Commit();
7367
}
7468
}
7569
}

src/Core/src/Platform/iOS/ContentView.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,14 @@ public override void LayoutSubviews()
2727
base.LayoutSubviews();
2828

2929
UpdateClip();
30+
31+
// iOS by default adds animations to certain actions such as layer resizing (setting the Frame property).
32+
// This can result in the background layer not keeping up with animations controlled by MAUI.
33+
// To prevent this undesired effect, native animations will be turned off for the duration of the operation.
34+
CATransaction.Begin();
35+
CATransaction.AnimationDuration = 0;
3036
this.UpdateMauiCALayer();
37+
CATransaction.Commit();
3138
}
3239

3340
internal IBorderStroke? Clip

src/Core/src/Platform/iOS/StrokeExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ internal static void UpdateMauiCALayer(this UIView platformView, IBorderStroke?
105105
{
106106
CALayer? backgroundLayer = platformView.Layer as MauiCALayer;
107107

108+
var initialRender = false;
108109
if (backgroundLayer == null)
109110
{
110111
backgroundLayer = platformView.Layer?.Sublayers?
111112
.FirstOrDefault(x => x is MauiCALayer);
112113

113114
if (backgroundLayer == null)
114115
{
116+
initialRender = true;
115117
backgroundLayer = new MauiCALayer
116118
{
117119
Name = ViewExtensions.BackgroundLayerName
@@ -122,6 +124,13 @@ internal static void UpdateMauiCALayer(this UIView platformView, IBorderStroke?
122124
}
123125
}
124126

127+
// While we're in the process of connecting the handler properties will not change
128+
// So it's useless to update the layer many times with the same value
129+
if (platformView is ContentView { View: null } && !initialRender)
130+
{
131+
return;
132+
}
133+
125134
if (backgroundLayer is MauiCALayer mauiCALayer)
126135
{
127136
backgroundLayer.Frame = platformView.Bounds;

0 commit comments

Comments
 (0)