【问题标题】:addSubview of composite views not working复合视图的 addSubview 不起作用
【发布时间】:2013-04-11 09:01:07
【问题描述】:

我有一个UIView,它由两个子视图组成-

@interface ANImageLabel : UIView

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UILabel *label;

- (id)initWithFrameAtOrigin:(CGPoint)origin imageViewSize:(CGSize)imageViewSize labelSize:(CGSize)labelSize;

@end

- (id)initWithFrameAtOrigin:(CGPoint)origin imageViewSize:(CGSize)imageViewSize labelSize:(CGSize)labelSize
{
    CGRect frame = CGRectZero;
    frame.origin = origin;

    // height should be the max of both heights
    CGFloat height = MAX(imageViewSize.height, labelSize.height);
    CGFloat width = imageViewSize.width + labelSize.width;
    imageViewSize.height = labelSize.height = frame.size.height = height;
    frame.size.width = width;

    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGRect imageViewFrame = CGRectZero;
        imageViewFrame.origin = origin;
        imageViewFrame.size = imageViewSize;
        self.imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
        self.imageView.contentMode = UIViewContentModeCenter;
        [self addSubview:self.imageView];

        CGRect labelFrame = CGRectZero;
        labelFrame.origin.x = origin.x + imageViewSize.width;
        labelFrame.origin.y = origin.y;
        labelFrame.size = labelSize;
        self.label = [[UILabel alloc] initWithFrame:labelFrame];
        [self addSubview:self.label];
    }
    return self;
}

alloc-init 这个视图的一个实例 (anImageLabelInstance) 在另一个视图 (mySuperView) 中,我想将它添加为子视图。 但是,如果我直接将此视图添加为子视图,则不起作用。

[mySuperView addSubview:anImageLabelInstance]; <--- DOESN'T WORK, WHY?

另一方面,如果我单独添加视图,它可以工作-

    [anImageLabelInstance.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
        [mySuperView addSubview:view];
    }];

addSubview的描述说

如果视图已经有一个超级视图并且该视图不是接收者, 这个方法在做receiver之前移除之前的superview 它的新超级视图。

但是,由于 anImageLabelInstance 在此操作之前最初并未作为子视图添加到任何其他视图中,为什么我需要单独添加视图?不应该使用addSubview 方法添加视图的所有子视图吗?

【问题讨论】:

  • “它不起作用”——这是什么意思?发生什么了?你的意思是你的视图没有出现?
  • 是的,所有子视图和您要添加的主视图都会被添加。
  • 尝试在您的班级中实施 - (void)dealloc 并在其中执行 NSLog。你的 NSLog 出现了吗?
  • 请同时发布实例化和使用您的 uiview 子类的代码。它是如何存储的?本地变量还是属性?

标签: ios objective-c iphone ios5 addsubview


【解决方案1】:

您似乎没有正确设置 2 个子视图的框架。 您将 origin 传递给 init 方法,可以设置 ANImageView 框架,但不能设置子视图。

假设您经过一个原点 (200, 200):ANImageLabel 视图将在您的 mySuperView 上 (200, 200) 放置。

如果您随后将 2 个子视图设置为相同的原点,它们将被放置在相对于 ANImageLabel 框架的坐标 (200, 200) 上,因此它们可能会落在实际视图框架之外。更准确地说

|''''''''''''''| -> your mySuperView
|              |
|              |
|              |
|              |
|    |''''|--- |----> you ANImageView on (200, 200)
|    |    |    |
|    ''''''    |
|              |
|           ---|-------> somewhere around here (but inside the ANImageView) you have your 2 
|              |         subviews, centered in relation to the ANImageViewFrame (so (400, 400)  
''''''''''''''''         in relation to the mySuperView)

在这种情况下,2 个子视图将在 ANImageView 的可见区域之外,因此被隐藏。当然,如果你随后单独拾取它们并将它们直接放在 mySuperView 中,它们的原点 (200, 200) 实际上将相对于 mySuperView 框架,并且可能在它的可见区域,所以没有隐藏。

试着转一下:

        CGRect imageViewFrame = CGRectZero;
        imageViewFrame.origin = origin;
        imageViewFrame.size = imageViewSize;
        self.imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
        self.imageView.contentMode = UIViewContentModeCenter;
        [self addSubview:self.imageView];

        CGRect labelFrame = CGRectZero;
        labelFrame.origin.x = origin.x + imageViewSize.width;
        labelFrame.origin.y = origin.y;
        labelFrame.size = labelSize;
        self.label = [[UILabel alloc] initWithFrame:labelFrame];
        [self addSubview:self.label];

进入这个:

    CGRect imageViewFrame = CGRectZero;
    imageViewFrame.origin = CGPointMake(0,0);
    imageViewFrame.size = imageViewSize;
    self.imageView = [[UIImageView alloc] initWithFrame:imageViewFrame];
    self.imageView.contentMode = UIViewContentModeCenter;
    [self addSubview:self.imageView];

    CGRect labelFrame = CGRectZero;
    labelFrame.origin.x = imageViewSize.width;
    labelFrame.origin.y = 0;
    labelFrame.size = labelSize;
    self.label = [[UILabel alloc] initWithFrame:labelFrame];
    [self addSubview:self.label];

看看有没有修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 1970-01-01
    • 2016-08-01
    • 2014-09-07
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多