【发布时间】:2018-05-05 06:34:12
【问题描述】:
我正在创建自定义 UIView,其中包含两个 UILabels 和 UIButton,因此我创建了一个子类来实现这一点。
现在流程如下:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
self.backgroundColor = [UIColor whiteColor];
self.settingsButton = [[UIButton alloc] initWithFrame:CGRectMake(frame.size.width - 2 * margin, frame.size.height - titleHeight - dateHeight, buttonSize, buttonSize)];
[self.settingsButton setImage:[UIImage imageNamed:@"settings_icon"] forState:UIControlStateNormal];
[self addSubview:self.settingsButton];
[self.settingsButton addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
return self;
}
按钮出现在 UIView 上,但不可点击。我尝试设置self.userInteractionEnabled = YES; 和一些东西但仍然没有结果:(
【问题讨论】:
-
检查developer.apple.com/library/content/documentation/…是否没有阻碍触摸的视图?
-
这看起来不错。其余的看起来如何?
-
确保按钮不在 UIView 的范围之外。简单测试:设置
clipsToBounds=YES,看看按钮是否仍然可见 -
@Larme @Wasserfloh 这个测试和调试器的测试显示按钮在
UIView的范围内所以我现在不知道 -
响应@Larme 的评论,按调试控制台上的View Hierarchy Debug 按钮,看看按钮顶部是否有其他视图
标签: ios objective-c iphone uiview uibutton