如何在UIStackView中更改UIButton背景颜色

时间:2018-12-25 10:49:34

标签: c# user-interface xamarin xamarin.ios mono

我正在尝试将预加载的UIStackView插入另一个StackView中。 在预加载的UIStackView中,我有以编程方式添加的子视图。 问题是,当我添加按钮并设置其颜色时,它们会变成白色背景,根本没有角半径

我尝试使用“背景=新的UIColor(51,181,229,255)” 和“ background = UIColor.FromCGColor(51,181,229,255)”

具有相同的结果。

        //TextFields
        UITextField oldPasswordTextField = new UITextField
        {
            Placeholder = "Old Password",
            BackgroundColor = UIColor.White,
            ClipsToBounds = true
        };

        UITextField newPasswordTextField = new UITextField
        {
            Placeholder = "New Password",
            BackgroundColor = UIColor.White,
            ClipsToBounds = true
        };

        UITextField repPasswordTextField = new UITextField
        {
            Placeholder = "Repeat New Password",
            BackgroundColor = UIColor.White,
            ClipsToBounds = true
        };

        //Buttons
        UIButton cancelButton = new UIButton();           
        cancelButton.SetTitle("Cancel", UIControlState.Normal);
        cancelButton.SetTitleColor(UIColor.White, UIControlState.Normal);
        cancelButton.BackgroundColor = UIColor.FromCGColor(new CoreGraphics.CGColor(51, 181, 229, 255));
        cancelButton.ClipsToBounds = true;

        UIButton submitButton = new UIButton();
        submitButton.SetTitle("Submit", UIControlState.Normal);
        submitButton.SetTitleColor(UIColor.White, UIControlState.Normal);
        //submitButton.BackgroundColor = new UIColor(51, 181, 229, 255);
        submitButton.ClipsToBounds = true;


        passwordChangeStackView.AddArrangedSubview(oldPasswordTextField);
        passwordChangeStackView.AddArrangedSubview(newPasswordTextField);
        passwordChangeStackView.AddArrangedSubview(repPasswordTextField);
        passwordChangeStackView.AddArrangedSubview(submitButton);
        passwordChangeStackView.AddArrangedSubview(cancelButton);
        passwordChangeStackView.AddArrangedSubview(activityIndicator);

MainStackView.InsertArrangedSubview(passwordChangeStackView,2); // 2->在相关按钮之后

我需要按钮具有我选择的颜色,白色文本, 并且所有子视图都带有圆角...但是我无法使两者都起作用。

2 个答案:

答案 0 :(得分:1)

您可以使用 background = new UIColor(51,181,229,255),但是此函数中的参数错误

  

RGB颜色值由rgb(红色,绿色,蓝色)指定。

     

每个参数(红色,绿色和蓝色)都定义了   颜色为0到255之间的整数。

     

例如,rgb(0,0,255)呈现为 blue ,因为蓝色   参数设置为最大值(255),其他设置为   0。

让我们看一下定义:

  //UIColor constructor from red, green, blue and alpha components.
        //
        // Parameters:
        //   red:
        //     Red component, 0.0 to 1.0f.
        //
        //   green:
        //     Green component 0.0 to 1.0f.
        //
        //   blue:
        //     Blue component value 0.0 to 1.0f.
        //
        //   alpha:
        //     Alpha (transparency) value from 0.0 to 1.0f.
        //
        // Remarks:
        //     This can be used from a background thread.
        [BindingImpl(BindingImplOptions.GeneratedCode | BindingImplOptions.Optimizable)]
        [Export("initWithRed:green:blue:alpha:")]
        public UIColor(nfloat red, nfloat green, nfloat blue, nfloat alpha);

因此,您可以使用:

submitButton.BackgroundColor = new UIColor(51/255.0f, 181/255.0f, 229/255.0f, 255/255.0f);

答案 1 :(得分:0)

必须使用UIColor.FromRGBA .....现在可以正常工作