合成視圖我有一個其由UIView
2 subviews-addSubview的不工作
@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
描述說
如果視圖已經有一個上海華和觀點而不是接收方, 此方法在使接收方 爲其新的超視圖之前移除先前的超視圖。
但是,由於anImageLabelInstance
最初並未作爲子視圖添加到此操作之前的任何其他視圖,爲什麼我需要單獨添加視圖?不應該使用addSubview
方法添加視圖的所有子視圖嗎?
「不起作用」 - 這是什麼意思?怎麼了?你的意思是你的觀點沒有出現? – occulus 2013-04-11 09:05:59
是的,所有子視圖都會添加以及添加的主視圖。 – occulus 2013-04-11 09:10:41
嘗試在你的類中實現 - (void)dealloc並在其中做一個NSLog。你的NSLog出現了嗎? – occulus 2013-04-11 09:11:43