2013-04-05 57 views
0

我正面臨一個非常奇怪的問題。我正在嘗試將UIbuttons添加到Monotouch中的UIscrollview。但UIbutton沒有得到顯示。無法添加UIbutton到單擊動態中的UIscrollview動態

下面是代碼:

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); 

相同的代碼工作,如果我將按鈕添加到視圖。 我可以得到一些這方面的幫助。

+0

您是否設置了UIScrollView.ContentSize? – 2013-04-05 09:16:17

回答

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); 

,它按預期工作。奇怪的事情似乎是你有7個RectangleF構造函數的參數。你剛剛嘗試將原點設置爲0,0來查看是否在那裏添加了一個按鈕?

+0

感謝您的回答。它現在有效。我把x和y的座標設置爲0,它的工作正常 – user2122350 2013-04-05 10:40:15

+0

嘿,只是一個小小的疑問。我已經使用\t \t爲相同的UIscrollview添加了一個UIImageView imageView.Frame = new System.Drawing.RectangleF(UIScreen.MainScreen.Bounds.Size.Width - 150,yadjustment,150,150); \t \t \t imageView.UserInteractionEnabled = true; \t \t \t imageView.Tag = position; \t \t \t \t \t \t scrollimages.AddSubview(imageView);圖像視圖被添加到循環旋轉的滾動視圖中。但同樣的框架,如果我給UIbutton它失敗。任何想法爲什麼? – user2122350 2013-04-05 11:14:16

+0

以何種方式失敗? – Blounty 2013-04-05 11:27:35