gpt4 book ai didi

ios - 在 ios 中添加多个 subview

转载 作者:行者123 更新时间:2023-11-28 20:12:13 24 4
gpt4 key购买 nike

我正在尝试以编程方式添加多个 subview ,但我在这些 subview 中的按钮不起作用。这些按钮也以编程方式添加为 subview 。

这是代码,希望能更多地解释我的意思。

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 的工具.如果您的任何按钮在父级框架之外,则不会检测到触摸。

关于ios - 在 ios 中添加多个 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774478/

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