我有一个子分类的父 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 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
方法。除了圆圈外,父对象被划定了罚款,即使我使用以下代码为 circle ,也可以正确显示。所以约束是可以的。
UIView circle = new UIView() { BackgroundColor = UIColor.Red };
我在做错什么?我不能覆盖两个Draw
方法(在子分类的父和子分类 circle 中)?
ps:我必须提到圆圈应该重叠一条线。但是Draw
从未被调用,因此似乎没有框架。
您是否知道您正在实例化uiview而不是该代码中的圆圈?
UIView circle = new UIView() { BackgroundColor = UIColor.Red };
另外,您不应该在Draw方法中添加子视图,因为它会被称为多个时间,实际上,您只能使用自定义绘制来覆盖Draw方法(这就是Circle View的情况,但不是父母视图的情况)。
来自Apple文档:
视图图是在需要的基础上发生的。首先显示视图时, 或者,由于布局的变化,它的全部或部分变为可见时, 系统要求该视图绘制其内容。对于包含的视图 使用Uikit或核心图形的自定义内容,系统调用 查看的抽签:方法
那么,您可以在实际添加父视图的位置发布代码狙击吗?和父视图的代码,您可能已经覆盖了一种方法,并且没有调用基类方法(例如setneedsdisplay或类似的内容),或者您没有添加视图。