gpt4 book ai didi

c# - 未调用子类 UIView 的绘制方法(另一个子类 UIView 的子类)

转载 作者:行者123 更新时间:2023-11-28 19:48:57 25 4
gpt4 key购买 nike

我有一个子类父 UIView 对象,它应该添加另一个子类 UIView。这是我要添加的 UIView 并且未调用 Draw 方法的地方:

public class Circle : UIView
{
private UIColor color;

public Circle ()
{
this.color = UIColor.Black;

this.BackgroundColor = UIColor.Clear;
}

public Circle (UIColor color)
{
this.color = color;

this.BackgroundColor = UIColor.Clear;
}

public override void Draw (CGRect rect)
{
base.Draw (rect);

// Get the context
CGContext context = UIGraphics.GetCurrentContext ();

context.AddEllipseInRect (rect);
context.SetFillColor (color.CGColor);
context.FillPath ();
}
}

这就是我添加圆圈的方式:

Circle circle = new Circle (UIColor.Red);
circle.TranslatesAutoresizingMaskIntoConstraints = false;
AddSubview (circle);

AddConstraint(NSLayoutConstraint.Create(circle, NSLayoutAttribute.Left, NSLayoutRelation.Equal, line, NSLayoutAttribute.Left, 1, 10));
AddConstraint(NSLayoutConstraint.Create(circle, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, line, NSLayoutAttribute.CenterY, 1, 0));
AddConstraint(NSLayoutConstraint.Create(circle, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 6));
AddConstraint(NSLayoutConstraint.Create(circle, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, 6));

上面的代码再次出现在父级的Draw 方法中。除了圆形外,父对象中的对象绘制得很好,即使我将下面的代码用作圆形,它也能正确显示。所以约束是可以的。

UIView circle = new UIView() { BackgroundColor = UIColor.Red };

我做错了什么?我不能覆盖两个 Draw 方法(在子类父类和子类 circle 中)吗?

PS:我不得不提一下,圆圈应该与一条线重叠。但是 Draw 从未被调用,所以它似乎没有得到一个框架。

最佳答案

您是否知道您正在实例化一个 UIView,而不是这段代码中的一个 Circle?

UIView circle = new UIView() { BackgroundColor = UIColor.Red };

此外,您不应该在 draw 方法中添加 subview ,因为它会被多次调用,实际上您应该只在进行自定义绘制时覆盖 Draw 方法(这是 Circle View 的情况,但不是父 View 的情况)。

来自苹果文档:

View drawing occurs on an as-needed basis. When a view is first shown, or when all or part of it becomes visible due to layout changes, the system asks the view to draw its contents. For views that contain custom content using UIKit or Core Graphics, the system calls the view’s drawRect: method

那么您可以在实际添加父 View 的地方发布代码截图吗?以及父 View 的代码,您可能已经覆盖了一个方法并且没有调用基类方法(如 setNeedsDisplay 或类似的方法)或者您没有添加 View 。

关于c# - 未调用子类 UIView 的绘制方法(另一个子类 UIView 的子类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30330530/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com