我是Monotouch的新手,并将以下代码添加到我的SingleViewApplication中。你可以在下面看到我的控制器代码,我在点击按钮后尝试更改背景。
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.
}
它对我来说很好,但我没有看到模拟器中的背景颜色发生变化。
有什么想法吗?谢谢。
答案 0 :(得分:3)
我也面临这个问题,但我解决了这个问题,希望这段代码可以帮到你
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);
答案 1 :(得分:0)
通过添加断点或控制台输出,确保您的代码实际在您的eventHandler中运行。如果这样做,也许你只是错过了“SetNeedsDisplay”。
试试这个:
private void buttonChangeColor_TouchUpInside (object sender, EventArgs e){
if (this.isRed) {
Console.WriteLine("isRed"); //Debug-Output
this.View.BackgroundColor = UIColor.LightGray;
this.isRed = false;
} else {
Console.WriteLine("isNotRed"); // Debug-Output
this.View.BackgroundColor = UIColor.Red;
this.isRed = true;
}
this.View.SetNeedsDisplay(); // Should force the View to redraw
}
答案 2 :(得分:0)
这个例子来自MonoTouch的一本书,我不得不将源代码转换成适用于MonoTouch的最新版本。
原始来源将subView称为
this.subView.BackgroundColor = UIColor.LightGray;
在这个过程中,我没有为视图创建一个插座,即subView。
如果没有创建插座,也可以按以下方式访问视图
this.View.SubViews[0].BackgroundColor = UIColor.LightGray
它解决了这个问题。