1

我有一个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将此视图 ( ) 的一个实例添加为子视图。但是,如果我直接将此视图添加为子视图,则它不起作用。anImageLabelInstancemySuperView

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

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

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

的描述addSubview

如果视图已经有一个超级视图并且该视图不是接收者,则此方法会在使接收者成为其新的超级视图之前删除先前的超级视图。

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

4

1 回答 1

3

看起来您没有正确设置 2 个子视图的框架。您将原点传递给 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];

看看它是否得到修复。

于 2013-04-11T10:27:13.430 回答