Xamarin IOS全球风格

时间:2017-12-14 14:41:53

标签: xamarin xamarin.ios

我正在开发Xamarin IOS应用程序。是否可以在Xamarin IOS应用程序中创建全局样式(例如按钮),如Xamarin Android App?有什么想法吗?

2 个答案:

答案 0 :(得分:3)

Apple提供了Appearance API,您可以将其用于此目的 在官方Xamarin开发人员门户网站上有一个nice article

它背后的想法是你可以在控件上使用静态方法来设置所需的外观,例如:

UIButton.Appearance.TintColor = UIColor.LightGray;
UIButton.Appearance.SetTitleColor(UIColor.FromRGB(0,127,14), UIControlState.Normal);

答案 1 :(得分:0)

在其中创建一个控件集通用主题。

例如

public class CustomTextField : UITextField
{
    public UIView RightViewEnabled
    {
        get;
        set;
    }

    public UIView RightViewDisabled
    {
        get;
        set;
    }

    public override CoreGraphics.CGRect LeftViewRect(CoreGraphics.CGRect forBounds)
    {
        var rect = base.LeftViewRect(forBounds);
        rect.X += 10;
        //rect.Y -= 2;
        return rect;
    }

    public override CoreGraphics.CGRect RightViewRect(CoreGraphics.CGRect forBounds)
    {
        var rect = base.RightViewRect(forBounds);
        rect.X -= 10;
        return rect;
    }

    public override CoreGraphics.CGRect TextRect(CoreGraphics.CGRect forBounds)
    {
        var rect = base.TextRect(forBounds);
        if (LeftView != null)
            rect.X += 5;
        if (RightView != null)
            rect.Width -= 10;
        return rect;
    }


    public override CoreGraphics.CGRect EditingRect(CoreGraphics.CGRect forBounds)
    {
        var rect = base.EditingRect(forBounds);
        if (LeftView != null)
            rect.X += 5;
        //if (RightView != null)
        //    rect.X -= 10;
        return rect;
    }

    public override bool Enabled
    {
        get 
        {
            return base.Enabled;
        }
        set
        {
            base.Enabled = value;
            if (value)
            {
                if (RightViewEnabled != null)
                    RightView = RightViewEnabled;
                base.TextColor = _textColor;
            }
            else
            {
                if (RightViewDisabled != null)
                    RightView = RightViewDisabled;
                if (TextColorDisabled != null)
                    base.TextColor = TextColorDisabled;
            }
        }
    }

    public UIColor TextColorDisabled
    {
        get;
        set;
    }

    private UIColor _textColor;

    public override UIColor TextColor
    {
        get
        {
            return base.TextColor;
        }
        set
        {
            _textColor = value;
            base.TextColor = value;
        }
    }

    public Guid ValueId
    {
        get;
        set;
    }

    public override bool CanPerform(ObjCRuntime.Selector action, Foundation.NSObject withSender)
    {
        if (action == new Selector("paste:") || action == new Selector("cut:") || action == new Selector("copy:")
             || action == new Selector("select:") || action == new Selector("selectAll:") || action == new Selector("delete:") || action == new Selector("_promptForReplace:")
             || action == new Selector("_transliterateChinese:") || action == new Selector("_showTextStyleOptions:") || action == new Selector("_define:") || action == new Selector("_addShortcut:")
             || action == new Selector("_accessibilitySpeak:") || action == new Selector("_accessibilitySpeakLanguageSelection:") || action == new Selector("_accessibilityPauseSpeaking:") || action == new Selector("makeTextWritingDirectionRightToLeft:")
             || action == new Selector("makeTextWritingDirectionLeftToRight:") || action == new Selector("_share:"))
            return false;
        else
            return base.CanPerform(action, withSender);
    }
}