我有一个子类父UIView
对象,它应该添加另一个子类UIView
。这是UIView
我想补充以及其中Draw
方法不叫:子类UIView(另一个子类UIView子)的绘制方法不叫
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
方法(在子类父类和子类圈)? PS:我必须指出,圆圈应该重叠一条线。但Draw
永远不会被调用,所以它似乎没有得到一个框架。
我把父母代码从'Draw'移到构造函数中,现在显示了圆圈!谢谢! – testing