C# 如何使用Apple button从登录中删除白色边缘

C# 如何使用Apple button从登录中删除白色边缘,c#,xaml,xamarin,xamarin.forms,xamarin.ios,C#,Xaml,Xamarin,Xamarin.forms,Xamarin.ios,问题-苹果官方的AsAuthorizationAppleId按钮显示白色边缘。我想把这些拿走 我尝试过的-边距=0,填充=0,并尝试设置拐角半径,但都不适用于我 实施-我已经在我的iOS应用程序中实现了使用Apple登录的功能,使用以下内容作为指导 在共享项目文件中有一个自定义按钮,在iOS项目中有一个使用苹果官方ASAuthorizationAppleIdButton的渲染器。这确保了它遵循苹果的按钮设计 我知道按钮是矩形的,但是我想去掉白色的边缘。非常感谢您的帮助 编辑制作: <con

问题-苹果官方的AsAuthorizationAppleId按钮显示白色边缘。我想把这些拿走

我尝试过的-边距=0,填充=0,并尝试设置拐角半径,但都不适用于我

实施-我已经在我的iOS应用程序中实现了使用Apple登录的功能,使用以下内容作为指导

在共享项目文件中有一个自定义按钮,在iOS项目中有一个使用苹果官方ASAuthorizationAppleIdButton的渲染器。这确保了它遵循苹果的按钮设计

我知道按钮是矩形的,但是我想去掉白色的边缘。非常感谢您的帮助

编辑制作:

<controls:AppleSignInButton SignIn="OnAppleLoginClicked" ButtonStyle="Black" Visual="None" CornerRadius="20" HorizontalOptions="FillAndExpand"
                                      VerticalOptions="CenterAndExpand" HeightRequest="50" IsVisible="{Binding IsAppleSignInAvailable}"/>
public class AppleSignInButton : Button
    {
        public AppleSignInButtonStyle ButtonStyle { get; set; }

        public event EventHandler SignIn;

        public AppleSignInButton()
        {
            Clicked += OnAppleSignInButtonClicked;
            Text = "Sign in with Apple";
            BorderWidth = 1;

            SetAppleButtonStyle();

            switch (ButtonStyle)
            {
                case AppleSignInButtonStyle.Black:
                    BackgroundColor = Color.Black;
                    TextColor = Color.White;
                    BorderColor = Color.Black;
                    break;
                case AppleSignInButtonStyle.White:
                    BackgroundColor = Color.White;
                    TextColor = Color.Black;
                    BorderColor = Color.White;
                    break;
                case AppleSignInButtonStyle.WhiteOutline:
                    BackgroundColor = Color.White;
                    TextColor = Color.Black;
                    BorderColor = Color.Black;
                    break;
            }
        }

        private void OnAppleSignInButtonClicked(object sender, EventArgs e)
        {
            SignIn?.Invoke(sender, e);
            Command?.Execute(CommandParameter);
        }

        private void SetAppleButtonStyle()
        {
            // check if we are in light or dark mode
            if (Application.Current.UserAppTheme == OSAppTheme.Light)
            {
                // use Black Apple button
                ButtonStyle = AppleSignInButtonStyle.Black;
            }
            else
            {
                // use White Apple button
                ButtonStyle = AppleSignInButtonStyle.WhiteOutline;
            }
        }

        public void InvokeSignInEvent(object sender, EventArgs e)
            => SignIn?.Invoke(sender, e);

        public void Dispose()
            => Clicked -= OnAppleSignInButtonClicked;
    }

    public enum AppleSignInButtonStyle
    {
        Black,
        White,
        WhiteOutline
    }
[assembly: ExportRenderer(typeof(MyAppName.Controls.AppleSignInButton), typeof(MyAppName.iOS.Renderers.AppleSignInButtonRenderer))]

namespace MyAppName.iOS.Renderers
{
    public class AppleSignInButtonRenderer : ViewRenderer<AppleSignInButton, UIView>
    {
        public static ASAuthorizationAppleIdButtonType ButtonType { get; set; } = ASAuthorizationAppleIdButtonType.Default;

        bool Is13 => UIDevice.CurrentDevice.CheckSystemVersion(13, 0);

        ASAuthorizationAppleIdButton button;
        UIButton oldButton;

        protected override void OnElementChanged(ElementChangedEventArgs<AppleSignInButton> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Cleanup
                if (Is13)
                {
                    if (button != null)
                        button.TouchUpInside -= Button_TouchUpInside;
                }
                else
                {
                    if (oldButton != null)
                        oldButton.TouchUpInside -= Button_TouchUpInside;
                }
            }

            if (e.NewElement != null)
            {
                // Create
                if (Is13)
                {
                    if (button == null)
                    {
                        button = (ASAuthorizationAppleIdButton)CreateNativeControl();
                        button.TouchUpInside += Button_TouchUpInside;

                        SetNativeControl(button);
                    }
                }
                else
                {
                    if (oldButton == null)
                    {
                        oldButton = (UIButton)CreateNativeControl();
                        oldButton.TouchUpInside += Button_TouchUpInside;
                        oldButton.Layer.CornerRadius = 4;
                        oldButton.Layer.BorderWidth = 1;
                        oldButton.ClipsToBounds = true;
                        oldButton.SetTitle(" " + Element.Text, UIControlState.Normal);

                        switch (Element.ButtonStyle)
                        {
                            case AppleSignInButtonStyle.Black:
                                oldButton.BackgroundColor = UIColor.Black;
                                oldButton.SetTitleColor(UIColor.White, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.Black.CGColor;
                                break;
                            case AppleSignInButtonStyle.White:
                                oldButton.BackgroundColor = UIColor.White;
                                oldButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.White.CGColor;
                                break;
                            case AppleSignInButtonStyle.WhiteOutline:
                                oldButton.BackgroundColor = UIColor.White;
                                oldButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.Black.CGColor;
                                break;
                        }

                        SetNativeControl(oldButton);
                    }
                }
            }
        }

        protected override UIView CreateNativeControl()
        {
            if (!Is13)
                return new UIButton(UIButtonType.Plain);
            else
                return new ASAuthorizationAppleIdButton(ButtonType, GetButtonStyle());
        }

        ASAuthorizationAppleIdButtonStyle GetButtonStyle()
        {
            switch (Element.ButtonStyle)
            {
                case AppleSignInButtonStyle.Black:
                    return ASAuthorizationAppleIdButtonStyle.Black;
                case AppleSignInButtonStyle.White:
                    return ASAuthorizationAppleIdButtonStyle.White;
                case AppleSignInButtonStyle.WhiteOutline:
                    return ASAuthorizationAppleIdButtonStyle.WhiteOutline;
            }

            return ASAuthorizationAppleIdButtonStyle.Black;
        }

        void Button_TouchUpInside(object sender, EventArgs e)
            => Element.InvokeSignInEvent(sender, e);
    }
}
XAML:

<controls:AppleSignInButton SignIn="OnAppleLoginClicked" ButtonStyle="Black" Visual="None" CornerRadius="20" HorizontalOptions="FillAndExpand"
                                      VerticalOptions="CenterAndExpand" HeightRequest="50" IsVisible="{Binding IsAppleSignInAvailable}"/>
public class AppleSignInButton : Button
    {
        public AppleSignInButtonStyle ButtonStyle { get; set; }

        public event EventHandler SignIn;

        public AppleSignInButton()
        {
            Clicked += OnAppleSignInButtonClicked;
            Text = "Sign in with Apple";
            BorderWidth = 1;

            SetAppleButtonStyle();

            switch (ButtonStyle)
            {
                case AppleSignInButtonStyle.Black:
                    BackgroundColor = Color.Black;
                    TextColor = Color.White;
                    BorderColor = Color.Black;
                    break;
                case AppleSignInButtonStyle.White:
                    BackgroundColor = Color.White;
                    TextColor = Color.Black;
                    BorderColor = Color.White;
                    break;
                case AppleSignInButtonStyle.WhiteOutline:
                    BackgroundColor = Color.White;
                    TextColor = Color.Black;
                    BorderColor = Color.Black;
                    break;
            }
        }

        private void OnAppleSignInButtonClicked(object sender, EventArgs e)
        {
            SignIn?.Invoke(sender, e);
            Command?.Execute(CommandParameter);
        }

        private void SetAppleButtonStyle()
        {
            // check if we are in light or dark mode
            if (Application.Current.UserAppTheme == OSAppTheme.Light)
            {
                // use Black Apple button
                ButtonStyle = AppleSignInButtonStyle.Black;
            }
            else
            {
                // use White Apple button
                ButtonStyle = AppleSignInButtonStyle.WhiteOutline;
            }
        }

        public void InvokeSignInEvent(object sender, EventArgs e)
            => SignIn?.Invoke(sender, e);

        public void Dispose()
            => Clicked -= OnAppleSignInButtonClicked;
    }

    public enum AppleSignInButtonStyle
    {
        Black,
        White,
        WhiteOutline
    }
[assembly: ExportRenderer(typeof(MyAppName.Controls.AppleSignInButton), typeof(MyAppName.iOS.Renderers.AppleSignInButtonRenderer))]

namespace MyAppName.iOS.Renderers
{
    public class AppleSignInButtonRenderer : ViewRenderer<AppleSignInButton, UIView>
    {
        public static ASAuthorizationAppleIdButtonType ButtonType { get; set; } = ASAuthorizationAppleIdButtonType.Default;

        bool Is13 => UIDevice.CurrentDevice.CheckSystemVersion(13, 0);

        ASAuthorizationAppleIdButton button;
        UIButton oldButton;

        protected override void OnElementChanged(ElementChangedEventArgs<AppleSignInButton> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Cleanup
                if (Is13)
                {
                    if (button != null)
                        button.TouchUpInside -= Button_TouchUpInside;
                }
                else
                {
                    if (oldButton != null)
                        oldButton.TouchUpInside -= Button_TouchUpInside;
                }
            }

            if (e.NewElement != null)
            {
                // Create
                if (Is13)
                {
                    if (button == null)
                    {
                        button = (ASAuthorizationAppleIdButton)CreateNativeControl();
                        button.TouchUpInside += Button_TouchUpInside;

                        SetNativeControl(button);
                    }
                }
                else
                {
                    if (oldButton == null)
                    {
                        oldButton = (UIButton)CreateNativeControl();
                        oldButton.TouchUpInside += Button_TouchUpInside;
                        oldButton.Layer.CornerRadius = 4;
                        oldButton.Layer.BorderWidth = 1;
                        oldButton.ClipsToBounds = true;
                        oldButton.SetTitle(" " + Element.Text, UIControlState.Normal);

                        switch (Element.ButtonStyle)
                        {
                            case AppleSignInButtonStyle.Black:
                                oldButton.BackgroundColor = UIColor.Black;
                                oldButton.SetTitleColor(UIColor.White, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.Black.CGColor;
                                break;
                            case AppleSignInButtonStyle.White:
                                oldButton.BackgroundColor = UIColor.White;
                                oldButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.White.CGColor;
                                break;
                            case AppleSignInButtonStyle.WhiteOutline:
                                oldButton.BackgroundColor = UIColor.White;
                                oldButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.Black.CGColor;
                                break;
                        }

                        SetNativeControl(oldButton);
                    }
                }
            }
        }

        protected override UIView CreateNativeControl()
        {
            if (!Is13)
                return new UIButton(UIButtonType.Plain);
            else
                return new ASAuthorizationAppleIdButton(ButtonType, GetButtonStyle());
        }

        ASAuthorizationAppleIdButtonStyle GetButtonStyle()
        {
            switch (Element.ButtonStyle)
            {
                case AppleSignInButtonStyle.Black:
                    return ASAuthorizationAppleIdButtonStyle.Black;
                case AppleSignInButtonStyle.White:
                    return ASAuthorizationAppleIdButtonStyle.White;
                case AppleSignInButtonStyle.WhiteOutline:
                    return ASAuthorizationAppleIdButtonStyle.WhiteOutline;
            }

            return ASAuthorizationAppleIdButtonStyle.Black;
        }

        void Button_TouchUpInside(object sender, EventArgs e)
            => Element.InvokeSignInEvent(sender, e);
    }
}
苹果在iOS项目中的登录按钮渲染器:

<controls:AppleSignInButton SignIn="OnAppleLoginClicked" ButtonStyle="Black" Visual="None" CornerRadius="20" HorizontalOptions="FillAndExpand"
                                      VerticalOptions="CenterAndExpand" HeightRequest="50" IsVisible="{Binding IsAppleSignInAvailable}"/>
public class AppleSignInButton : Button
    {
        public AppleSignInButtonStyle ButtonStyle { get; set; }

        public event EventHandler SignIn;

        public AppleSignInButton()
        {
            Clicked += OnAppleSignInButtonClicked;
            Text = "Sign in with Apple";
            BorderWidth = 1;

            SetAppleButtonStyle();

            switch (ButtonStyle)
            {
                case AppleSignInButtonStyle.Black:
                    BackgroundColor = Color.Black;
                    TextColor = Color.White;
                    BorderColor = Color.Black;
                    break;
                case AppleSignInButtonStyle.White:
                    BackgroundColor = Color.White;
                    TextColor = Color.Black;
                    BorderColor = Color.White;
                    break;
                case AppleSignInButtonStyle.WhiteOutline:
                    BackgroundColor = Color.White;
                    TextColor = Color.Black;
                    BorderColor = Color.Black;
                    break;
            }
        }

        private void OnAppleSignInButtonClicked(object sender, EventArgs e)
        {
            SignIn?.Invoke(sender, e);
            Command?.Execute(CommandParameter);
        }

        private void SetAppleButtonStyle()
        {
            // check if we are in light or dark mode
            if (Application.Current.UserAppTheme == OSAppTheme.Light)
            {
                // use Black Apple button
                ButtonStyle = AppleSignInButtonStyle.Black;
            }
            else
            {
                // use White Apple button
                ButtonStyle = AppleSignInButtonStyle.WhiteOutline;
            }
        }

        public void InvokeSignInEvent(object sender, EventArgs e)
            => SignIn?.Invoke(sender, e);

        public void Dispose()
            => Clicked -= OnAppleSignInButtonClicked;
    }

    public enum AppleSignInButtonStyle
    {
        Black,
        White,
        WhiteOutline
    }
[assembly: ExportRenderer(typeof(MyAppName.Controls.AppleSignInButton), typeof(MyAppName.iOS.Renderers.AppleSignInButtonRenderer))]

namespace MyAppName.iOS.Renderers
{
    public class AppleSignInButtonRenderer : ViewRenderer<AppleSignInButton, UIView>
    {
        public static ASAuthorizationAppleIdButtonType ButtonType { get; set; } = ASAuthorizationAppleIdButtonType.Default;

        bool Is13 => UIDevice.CurrentDevice.CheckSystemVersion(13, 0);

        ASAuthorizationAppleIdButton button;
        UIButton oldButton;

        protected override void OnElementChanged(ElementChangedEventArgs<AppleSignInButton> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                // Cleanup
                if (Is13)
                {
                    if (button != null)
                        button.TouchUpInside -= Button_TouchUpInside;
                }
                else
                {
                    if (oldButton != null)
                        oldButton.TouchUpInside -= Button_TouchUpInside;
                }
            }

            if (e.NewElement != null)
            {
                // Create
                if (Is13)
                {
                    if (button == null)
                    {
                        button = (ASAuthorizationAppleIdButton)CreateNativeControl();
                        button.TouchUpInside += Button_TouchUpInside;

                        SetNativeControl(button);
                    }
                }
                else
                {
                    if (oldButton == null)
                    {
                        oldButton = (UIButton)CreateNativeControl();
                        oldButton.TouchUpInside += Button_TouchUpInside;
                        oldButton.Layer.CornerRadius = 4;
                        oldButton.Layer.BorderWidth = 1;
                        oldButton.ClipsToBounds = true;
                        oldButton.SetTitle(" " + Element.Text, UIControlState.Normal);

                        switch (Element.ButtonStyle)
                        {
                            case AppleSignInButtonStyle.Black:
                                oldButton.BackgroundColor = UIColor.Black;
                                oldButton.SetTitleColor(UIColor.White, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.Black.CGColor;
                                break;
                            case AppleSignInButtonStyle.White:
                                oldButton.BackgroundColor = UIColor.White;
                                oldButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.White.CGColor;
                                break;
                            case AppleSignInButtonStyle.WhiteOutline:
                                oldButton.BackgroundColor = UIColor.White;
                                oldButton.SetTitleColor(UIColor.Black, UIControlState.Normal);
                                oldButton.Layer.BorderColor = UIColor.Black.CGColor;
                                break;
                        }

                        SetNativeControl(oldButton);
                    }
                }
            }
        }

        protected override UIView CreateNativeControl()
        {
            if (!Is13)
                return new UIButton(UIButtonType.Plain);
            else
                return new ASAuthorizationAppleIdButton(ButtonType, GetButtonStyle());
        }

        ASAuthorizationAppleIdButtonStyle GetButtonStyle()
        {
            switch (Element.ButtonStyle)
            {
                case AppleSignInButtonStyle.Black:
                    return ASAuthorizationAppleIdButtonStyle.Black;
                case AppleSignInButtonStyle.White:
                    return ASAuthorizationAppleIdButtonStyle.White;
                case AppleSignInButtonStyle.WhiteOutline:
                    return ASAuthorizationAppleIdButtonStyle.WhiteOutline;
            }

            return ASAuthorizationAppleIdButtonStyle.Black;
        }

        void Button_TouchUpInside(object sender, EventArgs e)
            => Element.InvokeSignInEvent(sender, e);
    }
}
[程序集:导出呈现程序(typeof(MyAppName.Controls.AppleSignInButton)、typeof(MyAppName.iOS.Renderers.AppleSignInButtonRenderer))]
命名空间MyAppName.iOS.Renderers
{

公共类AppleSignInButtonRenderer:ViewRenderer

感谢@Prasanth为我指明了正确的方向。 我希望按钮看起来是圆形的,但没有白色的边缘

要实现这一点,只需为按钮设置BackgroundColor=“Transparent”。 或者你可以将其设置为与视图的背景颜色相匹配,但我更喜欢透明


您能用Xaml向我展示您的完整代码吗?我无法复制我这边的问题。将Button backgroundColor设置为父背景色hi@JackHua MSFT我添加了更多代码,可以帮助您复制问题。Prasanth为我指出了正确的方向,答案是设置backgroundColor=“Transparent”。非常感谢您的帮助!@Prasanth非常感谢您为我指明了正确的方向!@paddystar很高兴听到您可以标记这个答案,这将帮助更多有同样问题的人:)。