【问题标题】:UIView Doesn't Appear and drawRect Method Never CalledUIView 不出现,drawRect 方法从未被调用
【发布时间】:2013-10-28 15:31:43
【问题描述】:

我创建了一个名为 MiniView 的 UIView 子类。

我尝试将它添加到我的 viewController 中,如下所示:

@interface SomeViewController ()

@property (strong, nonatomic) MiniView *miniView;

@end

- (void)viewDidLoad
        {
        [super viewDidLoad];

        self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
        _miniView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:_miniView];
    }

MiniView 类如下所示:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    NSLog(@"DRAW RECT");
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
    CGContextSetFillColorWithColor(context, redColor.CGColor);
    CGContextFillRect(context, self.bounds);
}

但是 drawRect 永远不会被调用,除非我在 setNeedsLayout 方法中明确调用它。它也没有绘制任何东西。我试过在 drawRect 方法中添加一个 UIImageView ,这看起来很好。但是上面的代码什么也没产生。

我也得到了错误:

:CGContextSetFillColorWithColor:无效的上下文 0x0。这是 一个严重的错误。此应用程序或它使用的库正在使用 无效的上下文,从而导致整体退化 系统的稳定性和可靠性。此通知是一种礼貌:请 解决这个问题。这将在即将到来的更新中成为致命错误。

如果我在drawRect方法中打印一条日志语句并输出'rect'值,它在200 x 200是正确的,所以我不知道为什么上下文是0x0。

所以一个问题是drawRect永远不会被调用,另一个问题是如果我显式调用它,什么都不会显示......

【问题讨论】:

  • 啊,我才意识到我做错了什么——我已经将 MiniView 类中的 setNeedsDisplay 方法重新定义为 NSLog 的东西。所以它的行为不正确。我给 [super setNeedsDisplay] 打了个电话,现在它可以工作了 slaps head

标签: objective-c uiview core-graphics drawrect cgcontext


【解决方案1】:

您可能需要在自定义 UIView 子类上调用 setNeedsDisplay

- (void)viewDidLoad {
    [super viewDidLoad];

    self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
    _miniView.backgroundColor = [UIColor blackColor];
    [_miniView setNeedsDisplay];    // Added this
    [self.view addSubview:_miniView];
}

这基本上是为了告诉系统您的UIView 需要重绘。请参阅the docs 了解更多信息。

【讨论】:

    【解决方案2】:

    根据要求作为单独的答案发布:

    实际的问题是我在 MiniView 类中重新定义了setNeedsDisplay 方法,如下所示:

    - (void)setNeedsDisplay
    {
        NSLog(@"Redrawing info: %@", [_info description]);
    }
    

    因为我忽略了对[super setNeedsDisplay] 的调用,所以drawRect 从未被调用过,也没有绘制任何东西。所以这修复了它:

    - (void)setNeedsDisplay
    {
        [super setNeedsDisplay];
        NSLog(@"Redrawing info: %@", [_info description]);
    }
    

    【讨论】:

      【解决方案3】:

      永远不要显式调用drawRect,试试这个:

      - (void)viewDidLoad
              {
              [super viewDidLoad];
      
              self.miniView = [[MiniView alloc] initWithFrame:CGRectMake(20.f, 20.f, 200.f, 200.f)];
              _miniView.backgroundColor = [UIColor blackColor];
              [self.view addSubview:_miniView];
      
              [_miniView setNeedsDisplay];
          }
      

      【讨论】:

      • 我在其他地方调用了 setNeedsDisplay,但如上所述,我意识到我在 MiniView 中重新定义了它,而无需调用 [super setNeedsDisplay]。不过感谢您的回答 - 它让我意识到我做错了什么。
      • @Smikey:您应该将其作为单独的答案发布,因为它与 Antonio MG 建议的不同。
      【解决方案4】:

      在你的drawrect方法中包括这一行:-

              [super drawRect:rect];
      

      【讨论】:

      • 在 UIView 的直接子类中,这不是必需的。 “这个方法的默认实现什么都不做。”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      相关资源
      最近更新 更多