【问题标题】:adding multiple subviews in ios在ios中添加多个子视图
【发布时间】:2013-11-04 18:25:55
【问题描述】:

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

这是代码,希望能进一步解释我的意思。

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;

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    您需要增加containerOfSelectedImage 的框架。目前宽度为0。

    containerOfSelectedImage 的框架必须足够大,这样按钮才能放入其中。如果按钮在该框架之外,它会显示出来,但你不能触摸他。

    要调试此问题,您可以使用Reveal App 之类的工具。如果您的任何按钮位于父级框架之外,则不会检测到触摸。

    【讨论】:

    • 非常感谢伙计,您为我节省了很多时间,过去 3 个小时我已经浪费了。问题解决了。
    • 没问题:)。当有人已经在那里时会更容易。请尽快将其标记为已接受,以便其他人也可以节省时间。
    【解决方案2】:

    [commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; 更改为[commentButton addTarget:self action:@selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

    【讨论】:

    • 好吧,也许UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];给你回一个不同的按钮,换成UIButton *commentButton = [[UIButton alloc] initWithFrame: CGRectMake(32.0f, y, 24.0f, 24.0f)];
    • 感谢您的时间伙伴,编写 UIView 的宽度问题解决了。
    • 是的,我没有看到 xD 行,但其他行同样重要。
    猜你喜欢
    • 2011-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多