سؤال

I have a UIView which is composed of two subviews-

@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;
}

I alloc-init an instance of this view (anImageLabelInstance) in another view (mySuperView) where I want to add it as a subview. However, if I add this view as a subview directly, it doesn't work.

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

If on the other hand, I add the views individually, it works-

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

The description of addSubview says

If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

However, since anImageLabelInstance is not originally added as a subview to any other view before this operation, why do I need to add the views individually? Shouldn't all the subviews of the view get added as well with an addSubview method?

هل كانت مفيدة؟

المحلول

It looks like you are not properly setting the frames of the 2 subviews. You pass the origin to the init method, and that is fine to set the ANImageView frame, but not the subviews.

So let's say you pass an origin point (200, 200): the ANImageLabel view will be put in you mySuperView on (200, 200).

If you then set the 2 subviews to the same origin point, they will be put on the coordinate (200, 200) in relation to the ANImageLabel frame, so they may fall outside the actual view frame. To be more precise

|''''''''''''''| -> 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)

In this case, the 2 subviews will be outside the visible area of the ANImageView, and thus hidden. Of course if you then pick them up singularly and you put them directly in mySuperView, their origin (200, 200) will actually be relative to mySuperView frame, and probably in its visible area, so not hidden.

Just try to turn this:

        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];

into this:

    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];

and see if it gets fixed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top