[英]adding multiple subviews in ios
我正在嘗試以編程方式添加多個子視圖,但是這些子視圖中的按鈕不起作用。 這些按鈕也以編程方式添加為子視圖。
這是代碼,希望能進一步解釋我的意思。
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(@"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:@"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
您需要增加containerOfSelectedImage
的框架。 當前寬度為0。
containerOfSelectedImage的框架必須足夠大,以便按鈕可以放入其中。 如果按鈕在該框架之外,則會顯示出來,但您無法觸摸他。
要調試此問題,可以使用Reveal App之類的工具。 如果您的任何按鈕位於父級邊框之外,則不會檢測到觸摸。
更改[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
到[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.