ios - 无法触摸自定义编程 UIView 中的 UIButton

标签 ios iphone uiview uiviewcontroller uibutton

我有 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/

相关文章:

ios - 出现 iTunes 登录提示时回调

ios - 从左到右更改导航标题位置

iphone - 如何检查用户是否打开了通知中心

ios - 在 [superview drawRect :]? 内调用 [subview setNeedsDisplay] 是否安全

iphone - 使用 ios Camera 以方形形式聚焦到对象?

ios - 在 Objective - C 中创建具有适当格式和图像的 PDF

ios - 通过 UIAutomation 在 UIDatePicker 中选择一个日期?

iphone - 使用谓词过滤 NSDictionary

ios - 核心图 - 使图形全屏显示的选项 - 呈现模态问题后添加 subview

android - 如何从 layout.xml 中删除默认的 cardview 边框