我在Xamarin中创建了一个带有支持类的View,并且想知道我是否可以使用CGRect大小创建该类的实例。我现在有以下代码:
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));
,我能用自己创建的菜单类做同样的事吗?我怎么能给Menu自己的构造函数,以便我可以做一些事情,var subView = new Menu(new CGRect(...));
?
答案 0 :(得分:1)
只是将uiview子类化并覆盖框架构造器
[Foundation.Register("Menu")]
public class Menu : UIView
{
public Menu(CGRect frame) : base(frame)
{
}
}