gpt4 book ai didi

ios - addTarget 不适用于 iOS 中的 UIButtons, View 被遮盖

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:42 25 4
gpt4 key购买 nike

我有两个 UIButton,我正在以编程方式创建并设置目标。如果我将这些按钮添加到主视图,一切都会很漂亮。但是,当我首先将其添加到 subview 并将该 subview 添加到主视图时,触摸事件不起作用。我猜这些按钮被遮住了。

如何在将这些按钮保留在 subview 中的同时揭开它们?

这是我的代码:

// Create button panel here
UIImage *buttonPanel = [UIImage imageNamed:@"left_button_background"];
UIImageView *buttonPanelView = [[UIImageView alloc] initWithImage:buttonPanel];
[buttonPanelView setFrame:CGRectMake(20, 186, 363, 345)];

// Populate the panel with buttons
UIButton *cmsButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 351, 142)];
[cmsButton setImage:[UIImage imageNamed:@"BTN_MATLIB"] forState:UIControlStateNormal];
[cmsButton addTarget:self action:@selector(loadCMS) forControlEvents:UIControlEventTouchDown];
[buttonPanelView addSubview:cmsButton];
[self setCms:cmsButton];

UIButton *calcButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 196, 351, 142)];
[calcButton setImage:[UIImage imageNamed:@"BTN_MORE_RESOURCES"] forState:UIControlStateNormal];
[calcButton addTarget:self action:@selector(loadCalc) forControlEvents:UIControlEventTouchDown];
[buttonPanelView addSubview:calcButton];
[self setCalc:calc];

[[self view] addSubview:buttonPanelView];

最佳答案

默认情况下, ImageView 不启用用户交互,这意味着它们的 subview 也不能接收用户交互。

buttonPanelView.userInteractionEnabled = YES;

关于ios - addTarget 不适用于 iOS 中的 UIButtons, View 被遮盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738158/

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