无法在Monotouch动态添加UI按钮到UIscrollview

时间:2013-04-05 08:54:41

标签: uiscrollview xamarin.ios

我面临一个非常奇怪的问题。我正在尝试将UI按钮添加到Monotouch中的UIscrollview。但是UI按钮没有显示出来。

以下是代码:

UIImage img = UIImage.FromFile("ecu.png");
UIButton btn = UIButton.FromType(UIButtonType.Custom);
btn.SetBackgroundImage(img,UIControlState.Normal);
btn.Frame = new RectangleF(UIScreen.MainScreen.Bounds.Size.Width - 185, 0,    185,UIScreen.MainScreen.Bounds.Size.Height, 100, 150, 150);
btn.TouchUpInside += (sender, e) => {
UIAlertView alert = new UIAlertView("Info","Tapped",null,"Ok",null);
            alert.Show();
        } ;
scrollimages.AddSubview (btn);

如果我将按钮添加到视图,相同的代码将起作用。 请问我可以得到一些帮助。

1 个答案:

答案 0 :(得分:0)

我在将UIScrollView添加到Interface Builder中的视图后尝试了以下代码:

    UIImage img = UIImage.FromFile("ecu.png");
        UIButton btn = UIButton.FromType(UIButtonType.Custom);
        btn.SetBackgroundImage(img,UIControlState.Normal);
        btn.Frame = new RectangleF(0.0f, 0.0f, 150.0f, 150.0f);
        btn.TouchUpInside += (sender, e) => {
            UIAlertView alert = new UIAlertView("Info","Tapped",null,"Ok",null);
            alert.Show();
        } ;
        this.ScrollImages.AddSubview (btn);

它按预期工作。奇怪的是,你有73个RectangleF构造函数的参数。你刚刚尝试将原点设置为0,0以查看是否添加了按钮吗?

相关问题