ios - 为什么我的自定义 View 没有出现在模拟器中

标签 ios uiview ios7 uibezierpath

这是我第一次处理自定义 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/

相关文章:

c++ - MRB_THROW 导致 libc++abi.dylib 出错

iphone - 如何创建自定义 iOS View

ios - 在旋转时调整 UIViewController 内的全屏 View 大小?

ios - 如何在 iOS Swift 中向多个 ViewController 添加一个公共(public) View ?

ios - AVCaptureDevice 的 isLowLightBoostSupported 在 5S iOS 7.1 上始终返回 false(用于自动启用 LowLightBoost WhenAvailable)

uinavigationcontroller - 在 iOS7 中从 UINavigationController 滑动弹出时查看 Controller 生命周期

ios - 类似 Instagram 的导航栏 (iOS 7)

ios - 如何从 SKScene 到 UIViewController?

ios - 如何让 iPhone X home bar 横屏双击?

ios - 两个 UIWebView 有单独的 cookie 存储?