簡體   English   中英

無法更改UISearchBar中的“取消”按鈕的標題

[英]Cannot change the title of the cancel button in the UISearchBar

我想使用MonoTouch更改UISearchBar的取消按鈕的標題。 是的,我已經看到了這種“ hack ”,但是我更喜歡一種更優雅的方式。 我正在嘗試使用:

UIButton.AppearanceWhenContainedIn(typeof(UISearchBar)).SetTitle("xxxx");

它應該可以在Objective-C中工作,但是在MonoTouch中卻出現以下錯誤

消息:錯誤CS1061:類型MonoTouch.UIKit.UIButton.UIButtonAppearance' does not contain a definition for SetTitle' MonoTouch.UIKit.UIButton.UIButtonAppearance' does not contain a definition for並且找不到SetTitle' of type MonoTouch.UIKit.UIButton.UIButtonAppearance'的擴展方法SetTitle' of type (您是否缺少using指令或組裝參考?)

我正在使用Xamarin Studio版本。 4

的hackish - Objective-C的代碼工作,因為目前執行的UIAppearance使用代理對象。 IOW蘋果公司不承諾保持實施相同,並且未記錄特定功能。

現在您已經知道了這一點,並且如果您仍然想與Xamarin.iOS一起使用它,那么您可以使用本問答中介紹的hack來實現。 這將允許您使用UIAppearance代理上的任何UIButton API。

為什么不使用更輕量的hack?

    public static void SetSearchBarCancelButtonTitle (UISearchBar sb, string title)
    {
        foreach (var subview in sb.Subviews)
            if (subview is UIButton)
                (subview as UIButton).SetTitle (title, UIControlState.Normal);
    }

UIButton不再似乎是UISearchBar的直接子視圖。 您必須像下面這樣遞歸,直到找到為止:

        public static bool SetButton(UIView parentView, string title, UIColor tint)
        {
            foreach (var sub in parentView.Subviews)
            {
                if (sub is UIButton)
                {
                    (sub as UIButton).SetTitle(title, UIControlState.Normal);
                    (sub as UIButton).SetTitleColor(tint, UIControlState.Normal);
                    return true;
                }

                if (SetButton(sub, title, tint))
                    return true;
            }

            return false;
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM