我在故事板中设置了以下结构。 我有一个UIViewController,其中包含一个UIView,其中包含按钮,文本和较小的UIView。 我想借助较小的UIView。
我目前的工作代码是:
public partial class MyDrawingController : UIViewController { public MyDrawingController (IntPtr handle) : base(handle) { } public override void LoadView () { base.LoadView (); View = new NewDrawingView (); } } public class NewDrawingView : UIView { public NewDrawingView () : base() {} public override void Draw (CGRect rect) { base.Draw (rect); DrawMe (rect); } public void DrawMe(CGRect rect) { // Drawing code } }这导致更大的UIView内容被我的绘图完全取代(覆盖我的文本和按钮)。
NewDrawingView = new NewDrawingView();虽然这会调用构造函数,但永远不会调用UIView的Draw方法,因此永远不会正确访问UIGraphics上下文来进行绘制。
编辑:
View.AddSubview(new NewDrawingView());也调用构造函数而不是Draw方法(并且似乎不显示新视图)。 如何在子视图中调用子视图的绘制方法或以其他方式绘制?
I have set up the following structure in my storyboard. I have a UIViewController that contains a UIView which contains buttons, text, and a smaller UIView. I would like to draw to the smaller UIView.
The current working code I have is:
public partial class MyDrawingController : UIViewController { public MyDrawingController (IntPtr handle) : base(handle) { } public override void LoadView () { base.LoadView (); View = new NewDrawingView (); } } public class NewDrawingView : UIView { public NewDrawingView () : base() {} public override void Draw (CGRect rect) { base.Draw (rect); DrawMe (rect); } public void DrawMe(CGRect rect) { // Drawing code } }This results in the larger UIView's content being entirely replaced by my drawing (covering my text and buttons).
NewDrawingView = new NewDrawingView();While this calls the constructor, the UIView's Draw method is never called, thereby never giving be correct access to the UIGraphics context to do drawing.
Edit:
View.AddSubview(new NewDrawingView());Also calls the constructor and not the Draw method (and doesn't appear to display a new view). How can I call the draw method of subview or otherwise draw within a subview?
最满意答案
终于找到了我需要的东西:
如何在monotouch / Xamarin.Ios中添加键盘上方的子视图?
public override void LoadView () { base.LoadView (); NewDrawingView drawingView = new NewDrawingView{ Frame = new RectangleF (0, 0, 100, 100) }; View.AddSubview (drawingView); }此代码正确初始化我的绘图视图,并在将子视图添加到视图控制器的主视图时调用draw方法。
Found what I needed at long last at:
How to add subview above the keyboard in monotouch/Xamarin.Ios?
public override void LoadView () { base.LoadView (); NewDrawingView drawingView = new NewDrawingView{ Frame = new RectangleF (0, 0, 100, 100) }; View.AddSubview (drawingView); }This code correctly initializes my drawing view and calls the draw method when adding the subview to the view controller's main view.
更多推荐
发布评论