Xamarin.ios iOS 13更改为UISearchBar色调';s、 can';我们不能达到同样的结果

Xamarin.ios iOS 13更改为UISearchBar色调';s、 can';我们不能达到同样的结果,xamarin.ios,uinavigationbar,uisearchbar,ios13,Xamarin.ios,Uinavigationbar,Uisearchbar,Ios13,我已经做了一整天的实验,试图弄清楚如何让我的UISearchBar在iOS13中显示与在iOS12/11中相同 因此,添加搜索栏的方式只是一个新的UISearchController var searchController = new UISearchController(searchResultsController: null); searchController.SearchBar.Placeholder = "Search";

我已经做了一整天的实验,试图弄清楚如何让我的UISearchBar在iOS13中显示与在iOS12/11中相同

因此,添加搜索栏的方式只是一个新的UISearchController

            var searchController = new UISearchController(searchResultsController: null);
            searchController.SearchBar.Placeholder = "Search";
            searchController.SearchResultsUpdater = this;
            searchController.HidesNavigationBarDuringPresentation = false;
            searchController.DimsBackgroundDuringPresentation = false;

            NavigationItem.SearchController = searchController;
            NavigationItem.HidesSearchBarWhenScrolling = false;
iOS 11/12上的结果:

iOS 13上的结果:

在iOS 13上,我使用新的UINavigationBarAppearance代码,如下所示:

                var appearance = new UINavigationBarAppearance();
                appearance.ConfigureWithOpaqueBackground();
                appearance.BackgroundColor = ColorPalette.TintColor;
                appearance.TitleTextAttributes = new UIStringAttributes { ForegroundColor = UIColor.White };

                NavigationItem.StandardAppearance = appearance;
在iOS 11/12上,我使用传统方式实现它:

                NavigationController.NavigationBar.BarStyle = UIBarStyle.Black;
                NavigationController.NavigationBar.TintColor = UIColor.White;
                NavigationController.NavigationBar.BarTintColor = ColorPalette.TintColor;
                NavigationController.NavigationBar.Translucent = false;
我已经尝试了很多方法,但似乎无法让UISearchBar自行着色iOS11/12是如何实现的


我知道新的UISearchBar现在可以访问UITextField,并且我可以配置背景色等。

iOS 13中添加了几个属性,因此您需要在条件设置的帮助下使用它们。要更改背景色,必须使用
搜索栏的
BackgroundColor
属性,如下所示

searchController.SearchBar.BackgroundColor = UIColor.Red;

使用自定义渲染器并以这种方式重写OnElementChanged方法

protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
{
  base.OnElementChanged(e);
  if (Control != null)
  {
    if(UIDevice.CurrentDevice.CheckSystemVersion(13,0))
      Control.SearchTextField.BackgroundColor = Element.BackgroundColor.ToUIColor();
  }
}
protected override void OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(控件!=null)
{
if(UIDevice.CurrentDevice.CheckSystemVersion(13,0))
Control.SearchTextField.BackgroundColor=Element.BackgroundColor.ToUIColor();
}
}

以后,您不必做任何其他事情,在ios 12和ios 13+上为我工作。在ios 13上,您可以通过
SearchTextField
属性访问内部
UISearchTextField
,您可以直接设置其背景(在我的情况下,我需要背景为白色)。确保检查iOS版本以避免异常

if(UIDevice.CurrentDevice.CheckSystemVersion(13,0))
{
    searchController.SearchBar.SearchTextField.BackgroundColor = UIColor.White;
}
上面的代码有一个副作用,即删除文本字段的角半径

延伸

extension UISearchBar {
  /// This solves iOS 13 issue which is a light gray overay covers the textField.
  /// - Parameter color: A color for searchField
  func setSearchFieldBackgroundColor(_ color: UIColor) {
    searchTextField.backgroundColor = color
    setSearchFieldBackgroundImage(UIImage(), for: .normal)
    // Make up the default cornerRadius changed by `setSearchFieldBackgroundImage(_:for:)`
    searchTextField.layer.cornerRadius = 10
    searchTextField.clipsToBounds = true
  }
}

您可以通过添加几行来获得所需的结果

searchBar.barTintColor=UIColor.red
searchBar.searchTextField.backgroundColor=UIColor.white

在您尝试此代码之前,请为搜索栏链接IB Outlets

@ibvar搜索栏:UISearchBar


是的,苹果公司似乎只推荐带有浅色背景的搜索栏,而浅色背景上的显示效果可能会更好。最后,
BarTintColor
似乎在IOS 13中不起作用。让我困惑的是,我根本找不到关于这一点的文档。是的,苹果关于新方法的文档太少了。我已经将其提交给GitHub。如果有Silion将在那里更新,你可以继续。现在有这个问题,很高兴其他人也注意到了。有人找到了这个问题的解决方案吗?如果你找到了,请在这里分享。
extension UISearchBar {
  /// This solves iOS 13 issue which is a light gray overay covers the textField.
  /// - Parameter color: A color for searchField
  func setSearchFieldBackgroundColor(_ color: UIColor) {
    searchTextField.backgroundColor = color
    setSearchFieldBackgroundImage(UIImage(), for: .normal)
    // Make up the default cornerRadius changed by `setSearchFieldBackgroundImage(_:for:)`
    searchTextField.layer.cornerRadius = 10
    searchTextField.clipsToBounds = true
  }
}