gpt4 book ai didi

ios - 复合 View 的 addSubview 不起作用

转载 作者:行者123 更新时间:2023-12-01 19:12:04 25 4
gpt4 key购买 nike

我有一个 UIView它由两个 subview 组成-

@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这个 View 的一个实例( anImageLabelInstance )在另一个 View ( mySuperView )中,我想将它添加为 subview 。
但是,如果我直接将此 View 添加为 subview ,则它不起作用。
[mySuperView addSubview:anImageLabelInstance]; <--- DOESN'T WORK, WHY?

另一方面,如果我单独添加 View ,它可以工作-
    [anImageLabelInstance.subviews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL *stop) {
[mySuperView addSubview:view];
}];
addSubview的描述说

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.



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

最佳答案

看起来您没有正确设置 2 个 subview 的框架。
您将原点传递给 init 方法,可以设置 ANImageView 框架,但不能设置 subview 。

因此,假设您通过了一个原点 (200, 200):ANImageLabel View 将放入您我的 super View 在 (200, 200)。

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

|''''''''''''''| -> 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 个 subview 将在 ANImageView 的可见区域之外,因此被隐藏。当然如果你再单独捡起来直接放到 我的 super View ,它们的原点 (200, 200) 实际上将相对于 我的 super View 框架,并且可能在其可见区域中,因此没有隐藏。

试着把这个:
        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];

看看它是否得到修复。

关于ios - 复合 View 的 addSubview 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15944650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com