如何在 Monotouch 中更改 UIButton 的背景颜色 [英] How to change background color of UIButton in Monotouch
本文介绍了如何在 Monotouch 中更改 UIButton 的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Monotouch 的新手,并在我的 SingleViewApplication 中添加了以下代码.你可以在下面看到我的控制器代码,我试图在点击按钮后更改背景.
I am newbie to Monotouch and have added the following code to my SingleViewApplication. And you can see my controller code below, where I am trying to change the background after click of the button.
public partial class FirstViewAppViewController : UIViewController
{
public FirstViewAppViewController () : base ("FirstViewAppViewController", null)
{
}
UIButton buttonChangeColor;
private void CreateButton (){
RectangleF viewFrame = this.View.Frame;
RectangleF buttonFrame = new RectangleF (10f, viewFrame.Bottom -
200f, viewFrame.Width - 20f, 50f);
this.buttonChangeColor = UIButton.FromType
(UIButtonType.RoundedRect);
this.buttonChangeColor.Frame = buttonFrame;
this.buttonChangeColor.SetTitle ("Tap to change view color",
UIControlState.Normal);
this.buttonChangeColor.SetTitle ("Changing color...",
UIControlState.Highlighted);
this.buttonChangeColor.TouchUpInside +=
this.buttonChangeColor_TouchUpInside;
this.View.AddSubview (this.buttonChangeColor);
}
bool isRed;
private void buttonChangeColor_TouchUpInside (object sender, EventArgs e){
if (this.isRed) {
this.View.BackgroundColor = UIColor.LightGray;
this.isRed = false;
} else {
this.View.BackgroundColor = UIColor.Red;
this.isRed = true;
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
this.CreateButton();
// Perform any additional setup after loading the view, typically from a nib.
}
对我来说看起来不错,但我没有看到模拟器中的背景颜色发生变化.
It looks fine to me, but I dont see the background color changing in simulator.
有什么想法吗?谢谢.
推荐答案
我也遇到这个问题但是我解决了这个问题希望这段代码对你有帮助
I am also face this problem but i solve this issue hope this code is help you
public static class SwapUIButtonColor
{
#region prop
public static UIButton PreviousButton {
get
{
return _previousButton
}
set
{
_previousButton = value;
}
}
public static UIButton CurrentButton
{
get
{
return _currentButton;
}
set
{
_currentButton = value;
}
}
public static bool SwapColor (UIButton sender, bool isPrevBakGraound)
{
if (isPrevBakGraound == false)
{
InitApplyColorBorder (sender);
SwapUIButtonColor.PreviousButton = sender;
return true;
}
else
{
if (SwapUIButtonColor.PreviousButton != sender)
{
InitApplyColorBorder (sender);
SwapUIButtonColor.CurrentButton = PreviousButton;
PreviousButton = sender;
SwapUIButtonColor.CurrentButton.Layer.BorderColor = (UIColor.Black).CGColor;
SwapUIButtonColor.CurrentButton.Layer.BorderWidth = 0f;
SwapUIButtonColor.CurrentButton.Layer.CornerRadius = 0f;
}
else if (SwapUIButtonColor.PreviousButton == sender)
{
InitApplyColorBorder (sender);
SwapUIButtonColor.PreviousButton = sender;
}
return true;
}
}
static void InitApplyColorBorder (UIButton sender)
{
sender.Layer.BorderColor = (UIColor.Red).CGColor;
sender.Layer.BorderWidth = 1.0f;
sender.Layer.CornerRadius = 10f;
}
也来自 ViewDidload 方法 在 UIButton 点击事件上调用上述方法
private bool isPrevBakGraound = false;
isPrevBakGraound = SwapUIButtonColor.SwapColor (btnReset, isPrevBakGraound);
这篇关于如何在 Monotouch 中更改 UIButton 的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文