【问题标题】:Xamarin iOS global stylesXamarin iOS 全局样式
【发布时间】:2020-07-28 19:12:13
【问题描述】:

我正在开发 Xamarin iOS 应用程序。

是否可以像在 Xamarin Android 应用中一样在 Xamarin iOS 应用中创建全局样式(例如按钮)?

有什么想法吗?

【问题讨论】:

  • Xamarin 文档中有一个专门针对全局样式的页面,我建议您在下次发布之前搜索文档,因为您会得到更快的答案:) developer.xamarin.com/guides/xamarin-forms/user-interface/…
  • @ElliotBlackburn,作者没有提到 xamarin 表单的用法。
  • @EvZ 公平点,我一定错过了,我的错,感谢您指出。

标签: xamarin xamarin.ios


【解决方案1】:

Apple 提供了您可以用于此目的的 Appearance API
在官方 Xamarin 开发者门户上有一个 nice article

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

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

【讨论】:

    【解决方案2】:

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

    例如

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多