如何在Xamarin iOS的UIViewController子视图中使用Core Graphics?(How to use Core Graphics in a UIViewController&#

编程入门 行业动态 更新时间:2024-10-26 14:38:57
如何在Xamarin iOS的UIViewController子视图中使用Core Graphics?(How to use Core Graphics in a UIViewController's Subview in Xamarin iOS?)

我在故事板中设置了以下结构。 我有一个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.

更多推荐

本文发布于:2023-08-06 18:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   如何在   iOS   UIViewController   Xamarin

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!