【问题标题】:Xamarin.Forms (iOS) - How to change the text of the SearchBar "Cancel" button?Xamarin.Forms (iOS) - 如何更改 SearchBar“取消”按钮的文本?
【发布时间】:2017-08-18 17:45:41
【问题描述】:

我们正在对我的团队正在开发的移动应用程序进行国际化,我注意到 SearchBar 中的“取消”按钮没有翻译(其他所有内容都可以翻译) - 无论我使用哪种语言,它总是用英语显示“取消”将 iPad 模拟器更改为。如果我可以自己设置文本,那么我可以确保它正确国际化。所以...

如何更改 Xamarin.Forms SearchBar 上的“取消”按钮文本?我尝试了自定义渲染器,但在定位取消按钮子视图时遇到了困难。 This answer 似乎很好地解释了如何在 Objective C 中做到这一点,但我无法在 Xamarin.Forms 框架中将其翻译成 C#。

【问题讨论】:

    标签: c# xamarin xamarin.ios xamarin.forms uisearchbar


    【解决方案1】:

    为 iOS 创建一个新的custom renderer。类似于 CustomSearchBarRenderer 和 Xamarin 的原始 SearchBarRenderer 的子类

    public class CustomSearchBarRenderer : SearchBarRenderer { }
    

    我可能会尝试覆盖OnElementChanged 方法并在基类完成所有工作后设置自定义标题。像这样:

    protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
    
        var cancelButton = Control.FindDescendantView<UIButton>();
        cancelButton.Title = "Whatever";
    }
    

    编辑:请注意,cancelButton 可能会在某些时候重新创建,因此您可能还必须在其他时候设置标题。不过,这应该是一个很好的起点。

    你也可以看看当前的 SearchBarRenderer 实现here

    【讨论】:

    • 几乎与我的设置完全相同 - 但您的 .FindDescendantView... 建议让我重新检查了 Control 上的可用方法,我发现 .Descendants!所以我要试试UIButton cancelButton = Control.Descendants().OfType&lt;UIButton&gt;().FirstOrDefault();
    • 所以我可以通过在 OnElementPropertyChanged() 方法中包含代码来更改文本,使用 UIButton cancelButton = Control.Descendants().OfType&lt;UIButton&gt;().FirstOrDefault(); 然后 cancelButton.SetTitle("Custom", UIControlState.Normal);*BUT* - 有一个错误,在第一次取消之后按钮出现,此后每次都有重叠的文本,其中自定义文本的第一部分也出现在“x”按钮(作为一部分)的右侧,就像这样:snag.gy/SiMsu5.jpg
    【解决方案2】:

    我结合了答案并在每个需要的事件上设置了按钮文本。我还修复了取消按钮的显示和隐藏(参见here):

    using FestivalHolledauApp.iOS;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Platform.iOS;
    using System.Linq;
    
    [assembly: ExportRenderer(typeof(SearchBar), typeof(CustomSearchBarRenderer))]
    namespace FestivalHolledauApp.iOS
    {
        public class CustomSearchBarRenderer : SearchBarRenderer
        {
            protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
            {
                base.OnElementChanged(e);
    
                // Fixing Cancel button
                if (e.NewElement != null)
                {             
                    this.Control.TextChanged += (s, ea) =>
                    {
                        this.Control.ShowsCancelButton = true;
                        SetCancelButtonText();
                    };
    
                    this.Control.OnEditingStarted += (s, ea) => //when control receives focus
                    {
                        this.Control.ShowsCancelButton = true;
                        SetCancelButtonText();
                    };
    
                    this.Control.OnEditingStopped += (s, ea) => //when control looses focus 
                    {
                        this.Control.ShowsCancelButton = false;
                    };
                }
            }
    
            private void SetCancelButtonText()
            {
                var cancelButton = Control.Descendants().OfType<UIButton>().FirstOrDefault();
                if (cancelButton != null)
                {
                    cancelButton.SetTitle("Schließen", UIControlState.Normal);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题...

      • UISearchBar 中的“取消”按钮
      • “返回”按钮可返回没有标题的前一页
      • 上下文菜单操作,例如“剪切”、“复制”和“粘贴”
      • 日期选择器和多行编辑器上的“完成”按钮

      我使用 resource based localization 本地化的所有其他内容。

      有很多黑客可以找到本地按钮并手动设置文本,但我知道这不是它的本意。于是我深入挖掘,发现了 Gerald Versluis 的以下优秀文章:

      对我来说,这只是意味着将以下块添加到我的 Info.plist:

          <key>CFBundleLocalizations</key>
          <array>
              <string>en</string>
              <string>de</string>
          </array>
      

      这样,iOS 会自动翻译当前系统文化的所有 UI 元素(无需将这些文本添加到任何资源文件中)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多