我有 MasterChatViewController
,我在其中添加了一个名为 JFShapedNavBar
的自定义 UIView 作为 subview 。在 JFShapedNavBar
中,我添加了一个带有图像的 UIButton
subview ,并向一个方法添加了一个目标操作,该方法只是将一些内容打印到控制台以便立即进行调试。
MasterChatViewController
包含 JFShapedNavBar
和
JFShapedNavBar
包含 UIButton
。
我使用 Masonry
来设置约束。当我初始化 View 时,我使用 initWithFrame:CGRectZero
的自定义变体:
_shapedNavBar = [[JFShapedNavBar alloc] initWithFrame:CGRectZero
forViewController:self];
[self.view addSubview:_shapedNavBar];
[_shapedNavBar mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.equalTo(self.view);
}];
在 JFShapedNavBar.m
中:
-(void)configureRightButton {
// Right Button of Nav
UIImage *buttonImage = [UIImage imageNamed:@"LobbyButton"];
_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rightButton setImage:buttonImage forState:UIControlStateNormal];
[_rightButton addTarget:self action:@selector(testTouch) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_rightButton];
[self setNeedsUpdateConstraints];
}
-(void)testTouch {
NSLog(@"You managed to figure out what you were doing wrong.");
}
-(void)updateConstraints {
[super updateConstraints];
[_arcShadow mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.trailing.equalTo(self);
make.height.equalTo(__navHeight);
}];
[_arcDrop mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(_arcShadow);
make.height.equalTo(_arcShadow.mas_height);
}];
[_rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(24);
make.trailing.equalTo(self).offset(-16);
make.height.equalTo(@30);
}];
}
最终,我试图创建一个自定义导航栏(但不是 UIKit 中导航栏的子类)。我试验了大约 2 个小时,试图找出目标操作处理程序不触发的原因,但现在我很沮丧,没有想法。为什么我不能触摸 UIButton?
也许这很重要,我不知道,但在我的 View Controller MasterChatViewController
上,我设置了隐藏的导航栏 self.navigationController.navigationBarHidden = YES;
最佳答案
这不会解决您的问题,而只是一些用于调试可点击问题的标准操作
1)暂时将所有按钮的背景设置为绿色(这样你就可以看到它们的点击区域延伸到了哪里
2) 在 child 和 super View 容器上设置 userInteraction = YES(有时)
3) 留意任何可能吞没你触摸的手势识别器
4) 确保容器 View 内的按钮在其边界内..如果不是,它将不可点击
5) 要进一步调查布局问题,请使用 Reveal 或内置的 xcodes 来调试 View http://www.raywenderlich.com/98356/view-debugging-in-xcode-6
关于ios - 无法触摸自定义编程 UIView 中的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108615/