如何在MonoTouch中为继承的视图创建UIAppearance代理?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在MonoTouch中为继承的视图创建UIAppearance代理?相关的知识,希望对你有一定的参考价值。

我创建了一个UIButton的子类,它充当UIBarButtonItem中的自定义Back按钮:

 - - - - - - - - - - - - - - - - - 
|         UIBarButtonItem         |

|     ------------------------    |
     /                       |
|     BackButton : UIButton |    |
      ------------------------
|  _  _  _  _  _  _  _  _  _  _  _|

当我将我的代码切换到使用UIAppearance时,我注意到BackButton.Appearance是毫无疑问的,继承自UIButton.Appearance,因此更改它将改变整个应用程序中的所有UIButtons,而不仅仅是我的自定义按钮。

我知道我可以使用AppearanceWhenContainedIn但是,因为UIBarButtonItem不是外观容器,我必须在它们之间插入另一个视图以识别后退按钮,我不想这样做。

如何为我的自定义UIAppearance子类提供UIButton代理,该子类具有UIButton.UIButtonAppearance中的所有可用方法?我注意到它的构造函数是内部的。

答案

虽然可能有一个更简单的答案,但我最终直接从UIAppearance继承并从MonoDevelop C#反汇编程序中复制粘贴相关的UIButtonUIButton+UIButtonAppearance绑定实现,并在此处和那里修复内部字段。

BackButton内:

static readonly IntPtr class_ptr = Class.GetHandle(typeof(BackButton));

public new static BackButtonAppearance Appearance
{
    get {
        return new BackButtonAppearance (Messaging.IntPtr_objc_msgSend (class_ptr, UIAppearance.SelectorAppearance));
    }
}

public new static BackButtonAppearance AppearanceWhenContainedIn (params Type[] containers)
{
    return new BackButtonAppearance (UIAppearance.GetAppearance (class_ptr, containers));
}

嵌套的BackButtonAppearance

public class BackButtonAppearance : UIAppearance {
    public BackButtonAppearance(IntPtr handle) : base(handle) { }

    // These are copied from UIButton disassembly:

    static readonly IntPtr selSetBackgroundImageForState_ = Selector.GetHandle ("setBackgroundImage:forState:");
    static readonly IntPtr selSetTitleShadowColorForState_ = Selector.GetHandle ("setTitleShadowColor:forState:");            
    static readonly IntPtr selSetTitleColorForState_ = Selector.GetHandle ("setTitleColor:forState:");

    // These are copied from UIButton+UIButtonAppearance disassembly,
    // with internal UIButton.selSet* fields replaced by fields declared above.

    [Export ("setBackgroundImage:forState:"), CompilerGenerated]
    public virtual void SetBackgroundImage (UIImage image, UIControlState forState)
    {
        UIApplication.EnsureUIThread ();
        if (this.IsDirectBinding) {
            Messaging.void_objc_msgSend_IntPtr_UInt32 (base.Handle, selSetBackgroundImageForState_, (image != null) ? image.Handle : IntPtr.Zero, (uint)forState);
        } else {
            Messaging.void_objc_msgSendSuper_IntPtr_UInt32 (base.SuperHandle, selSetBackgroundImageForState_, (image != null) ? image.Handle : IntPtr.Zero, (uint)forState);
        }
    }


    [Export ("setTitleColor:forState:"), CompilerGenerated]
    public virtual void SetTitleColor (UIColor color, UIControlState forState)
    {
        UIApplication.EnsureUIThread ();
        if (this.IsDirectBinding) {
            Messaging.void_objc_msgSend_IntPtr_UInt32 (base.Handle, selSetTitleColorForState_, (color != null) ? color.Handle : IntPtr.Zero, (uint)forState);
        } else {
            Messaging.void_objc_msgSendSuper_IntPtr_UInt32 (base.SuperHandle, selSetTitleColorForState_, (color != null) ? color.Handle : IntPtr.Zero, (uint)forState);
        }
    }

    [Export ("setTitleShadowColor:forState:"), CompilerGenerated]
    public virtual void SetTitleShadowColor (UIColor color, UIControlState forState)
    {
        UIApplication.EnsureUIThread ();
        if (this.IsDirectBinding) {
            Messaging.void_objc_msgSend_IntPtr_UInt32 (base.Handle, selSetTitleShadowColorForState_, (color != null) ? color.Handle : IntPtr.Zero, (uint)forState);
        } else {
            Messaging.void_objc_msgSendSuper_IntPtr_UInt32 (base.SuperHandle, selSetTitleShadowColorForState_, (color != null) ? color.Handle : IntPtr.Zero, (uint)forState);
        }
    }
}

这允许我带来我的自定义视图以支持UIAppearance风格的API。

另一答案

为完整性添加答案。

使用BackButton.GetAppearance<BackButton>()而不是BackButton.Appearance

以上是关于如何在MonoTouch中为继承的视图创建UIAppearance代理?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 monotouch 在 uitableviewcell 中添加 uipickerview 作为子视图?

如何使用 Monotouch 在 iPad 故事板应用程序中以编程方式管理视图

这是 MonoTouch GC 中的错误吗?

MonoTouch:如何从 TabBarController 的多个视图中选择初始视图

Monotouch:如何使用 NavigationController.PushViewController 更改视图更改的默认动画

如何使用 MonoTouch 在 iPhone 上播放视频?