2017-03-21 202 views
0

我已經在Xamarin中創建了一個具有支持類的視圖,並且想知道是否可以使用CGRect尺寸創建該類的實例。我有以下代碼現在:創建自定義尺寸視圖Xamarin.iOS

using System; 
using CoreGraphics; 
using Foundation; 
using UIKit; 

namespace SimpleScroll.iOS 
{ 
    public partial class ViewController : UIViewController 
    { 
     public ViewController(IntPtr handle) : base(handle) 
     { 

     } 

     public override void ViewDidLoad() 
     { 
      base.ViewDidLoad(); 

      // Perform any additional setup after loading the view, typically from a nib. 

      // UIScrollView with same width and height as ViewController 
      var mainScrollView = new UIScrollView(new CGRect(0, 0, this.View.Frame.Size.Width, this.View.Frame.Size.Height)); 

      // Enable pagination and set other attributes 
      mainScrollView.PagingEnabled = true; 
      mainScrollView.ShowsVerticalScrollIndicator = false; 
      mainScrollView.ShowsHorizontalScrollIndicator = false; 
      mainScrollView.Bounces = false; 

      int numberOfViews = 2; 
      for (int i = 0; i < numberOfViews; i++) 
      { 
       nfloat xOrigin = i * this.View.Frame.Size.Width; 
       var subView = new UIView(new CGRect(xOrigin, 0, this.View.Frame.Size.Width, this.View.Frame.Size.Height)); 
       subView.BackgroundColor = UIColor.FromRGBA(0.5f/i, 0.5f, 0.5f, 1); 
       mainScrollView.AddSubview(subView); 
      } 

      mainScrollView.ContentSize = new CGSize(this.View.Frame.Size.Width * numberOfViews, this.View.Frame.Size.Height); 
      this.View.AddSubview(mainScrollView); 
     } 

     public override void DidReceiveMemoryWarning() 
     { 
      base.DidReceiveMemoryWarning(); 
      // Release any cached data, images, etc that aren't in use.  
     } 
    } 
} 

相反的var subView = new UIView(new CGRect(xOrigin, 0, this.View.Frame.Size.Width, this.View.Frame.Size.Height));,我就能夠做我自己的菜單類,我創建了同樣的事情?我怎麼能給菜單自己的構造函數,以便我可以做些什麼,var subView = new Menu(new CGRect(...));

回答

1

剛子類的UIView和覆蓋框架constuctor

[Foundation.Register("Menu")] 
public class Menu : UIView 
{ 
    public Menu(CGRect frame) : base(frame) 
    { 

    } 
} 
+0

然後裏面就寫:'返回新菜單(新的CGRect(...));'? –

+0

沒有沒有在新班級裏,但你在哪裏使用它用新菜單替換新的新UIView(...) – svn

+0

非常感謝你 –