繁体   English   中英

无法触摸自定义程序化UIView中的UIButton

[英]Can't touch UIButton inside custom programmatic UIView

我有MasterChatViewController ,其中添加了名为JFShapedNavBar的自定义UIView作为子视图。 JFShapedNavBar内部,我添加了带图像的UIButton子视图,并向仅向控制台打印内容以便立即进行调试的方法添加了target-action。

MasterChatViewController包含JFShapedNavBar JFShapedNavBar包含UIButton

我使用Masonry来设置约束。 当我初始化视图时,我使用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?

也许这很重要,我不知道,但是在我的视图控制器MasterChatViewController我已隐藏了导航栏self.navigationController.navigationBarHidden = YES;

这不会解决您的问题,而只是调试可轻敲问题的一些标准操作

1)暂时将所有按钮的背景设置为绿色(这样您就可以看到其点击区域扩展到了

2)在子级和超级容器上设置userInteraction = YES(有时)

3)注意任何可能会吞噬您的触摸的手势识别器

4)确保容器视图内的按钮在其边界之内..如果不是,则将其不可点击

5)要进一步调查布局问题,请使用Reveal或内置rip的xcode进行视图调试http://www.raywenderlich.com/98356/view-debugging-in-xcode-6

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM