【问题标题】:Problems creating a custom UIView创建自定义 UIView 的问题
【发布时间】:2010-01-20 01:48:36
【问题描述】:

我正在尝试创建自己的 UIView 子类。我通过拖出 UIView 将其放置在 Interface Builder 的视图中,然后在 Class Identity 字段中指定我的子类的名称。然而我的 UIView 并没有自己绘制。

这里——在一个简化的例子中——是我的 UIView 子类中的代码:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder])
    {
        UILabel* label = [[[UILabel alloc] initWithFrame:self.frame] autorelease];
        label.text = @"Hello.";
        label.textColor = [UIColor whiteColor];
        [self addSubview:label];        
    }

    return self;
}

我已经看到了对覆盖 drawRect: 的引用,但老实说,我不知道在该方法中我会做什么。我确定我在做一些明显错误的事情,但我不知道是什么。

任何建议将不胜感激!

谢谢。

【问题讨论】:

    标签: iphone uiview custom-controls


    【解决方案1】:

    尝试使用self.bounds 而不是self.frame

    UILabel* label = [[[UILabel alloc] initWithFrame:self.bounds] autorelease];
    

    您的视图框架可能不在原点 {0 0},这意味着标签最终会超出您视图的可见区域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多