【问题标题】:why isn't my custom view showing up in simulator为什么我的自定义视图没有出现在模拟器中
【发布时间】:2023-10-24 17:26:01
【问题描述】:

这是我第一次处理自定义视图(所以请原谅我的无知),所以我试图弄清楚这是否应该工作(或者为什么不工作)。我试图通过执行 New -> File 并将父级设置为 UIView 来创建自定义视图(称为 IAMax)。我删除了 initWithFrame 并将以下内容添加到 drawRect:

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    NSLog(@"here i am");
    UIBezierPath *aPath = [UIBezierPath bezierPath];
    // Set the starting point of the shape.
    [aPath moveToPoint:CGPointMake(100.0, 0.0)];
    // Draw the lines.
    [aPath addLineToPoint:CGPointMake(200.0, 40.0)];
    [aPath addLineToPoint:CGPointMake(160, 140)];
    [aPath addLineToPoint:CGPointMake(40.0, 140)];
    [aPath addLineToPoint:CGPointMake(0.0, 40.0)];
    [aPath closePath];
}

我转到我的主情节提要并将通用 UIView 拖到我的主情节提要中,并将 Class 切换为 IAMax(并将背景颜色切换为绿色)。当我在模拟器中运行时,我什么也看不到(但我确实在控制台中看到了 NSLog 语句)。我究竟做错了什么?

谢谢

【问题讨论】:

    标签: ios uiview ios7 uibezierpath


    【解决方案1】:

    创建路径后你需要绘制它:

    [[UIColor redColor] set];
    [aPath stroke];
    

    这会将路径绘制为红色。

    【讨论】:

    • 啊,非常感谢!我以为我在 UIView 中做错了 - 必须等待几分钟。
    最近更新 更多