这是我第一次处理自定义 View (所以请原谅我的无知),所以我试图弄清楚这是否应该起作用(或者为什么不起作用)。我尝试通过执行“新建”->"file"并将父级设置为 UIView 来创建自定义 View (称为 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];
}
我转到我的主 Storyboard,将通用 UIView 拖到我的主 Storyboard中,并将类切换为 IAMax(并将背景颜色切换为绿色)。当我在模拟器中运行时,我没有看到任何东西(但我确实在控制台中看到了 NSLog 语句)。我究竟做错了什么?
谢谢
最佳答案
创建路径后,您需要绘制它:
[[UIColor redColor] set];
[aPath stroke];
这会将路径绘制为红色。
关于ios - 为什么我的自定义 View 没有出现在模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20132370/