Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[iOS] Specify a default size for UISearchBar width if needed (#3635) f…
Browse files Browse the repository at this point in the history
…ixes #3413 fixes #2139

* [Controls] Add reproduction and test case for issue #3413

* [iOS] Don't use max value for iOS UISearchbar breaks on iOS 11.3

* [iOS] Force width value so we can renderer UISearchBar on iOS10 with infinite width

* [Controls] Fix spelling and add Manual Review to the test of #3413

* [iOS] Simplify code for UISearchBar width

* [iOS] Return always some width from measure the UISearchBar on IOS

* [Controls] Add reproduction case for issue #2139
  • Loading branch information
rmarinho authored Sep 3, 2018
1 parent a8260b0 commit 6c94e6e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;

#if UITEST
using Xamarin.UITest;
using NUnit.Framework;
using Xamarin.Forms.Core.UITests;
#endif

namespace Xamarin.Forms.Controls.Issues
{
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 3413, "[iOS] Searchbar in Horizontal Stacklayout doesn't render", PlatformAffected.iOS)]
public class Issue3413 : TestContentPage
{
protected override void Init()
{
Padding = new Thickness(20);

var layout = new StackLayout
{
Orientation = StackOrientation.Vertical
};

var searchBar = new SearchBar
{
BackgroundColor = Color.Yellow,
Text = "i m on a vertical stacklayout",
AutomationId = "srb_vertical"
};
layout.Children.Add(new Label { Text = "Vertical" });
layout.Children.Add(searchBar);

var layout1 = new StackLayout
{
Orientation = StackOrientation.Horizontal
};

var searchBar1 = new SearchBar
{
BackgroundColor = Color.Yellow,
Text = "i m on a horizontal stacklayout",
AutomationId = "srb_horizontal"
};

layout1.Children.Add(new Label { Text = "Horizontal" });
layout1.Children.Add(searchBar1);

var searchBar2 = new SearchBar
{
BackgroundColor = Color.Blue,
Text = "i m with expand",
HorizontalOptions = LayoutOptions.CenterAndExpand,
AutomationId = "srb_grid"
};

var grid = new Grid();
grid.Children.Add(layout);
Grid.SetRow(layout, 0);
grid.Children.Add(layout1);
Grid.SetRow(layout1, 1);
grid.Children.Add(searchBar2);
Grid.SetRow(searchBar2, 2);
Content = grid;
}

#if UITEST
[Test]
[Category(UITestCategories.ManualReview)]
public void Issue3413Test ()
{
RunningApp.WaitForElement (q => q.Marked ("srb_vertical"));
RunningApp.WaitForElement (q => q.Marked ("srb_horizontal"));
RunningApp.Screenshot ("Please verify we have 2 SearchBars. One below the label, other side by side with the label");
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Import_RootNamespace>Xamarin.Forms.Controls.Issues</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Issue2894.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2894.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3524.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue2004.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3333.cs" />
Expand Down Expand Up @@ -772,6 +772,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue2728.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue1667.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3012.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3413.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue3525.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
16 changes: 12 additions & 4 deletions Xamarin.Forms.Platform.iOS/Renderers/SearchBarRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,18 @@ protected override void SetBackgroundColor(Color color)

public override CoreGraphics.CGSize SizeThatFits(CoreGraphics.CGSize size)
{
if (nfloat.IsInfinity(size.Width) && Forms.IsiOS11OrNewer)
size.Width = nfloat.MaxValue;

return base.SizeThatFits(size);
if (nfloat.IsInfinity(size.Width))
size.Width = (nfloat)(Element?.Parent is VisualElement parent ? parent.Width : Device.Info.ScaledScreenSize.Width);

var sizeThatFits = Control.SizeThatFits(size);

if (Forms.IsiOS11OrNewer)
return sizeThatFits;

////iOS10 hack because SizeThatFits always returns a width of 0
sizeThatFits.Width = (nfloat)Math.Max(sizeThatFits.Width, size.Width);

return sizeThatFits;
}

void OnCancelClicked(object sender, EventArgs args)
Expand Down

0 comments on commit 6c94e6e

Please sign in to comment.