当我使用 initWithTitle 创建 UIBarButtonItem 时,它不会响应标题内部的触摸,只有标题外部才



这里是代码

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:@"Interpretations" style:UIBarButtonItemStyleBordered target:self action:@selector(showInterpretations:)];
self.navigationItem.rightBarButtonItem = button;

在我的程序中,当我创建一个按钮并设置它的标题时,titleLabel.userInteractionEnabled的默认值是YES,这会导致同样的问题,所以我必须UserInteractionEnabled设置为NO,以便按钮可以正确响应。

但是在导航栏中,UIBarButtonItem的类不是UIView,我尝试使用initWithImage创建UIBarButtonItem并且效果很好,我认为使用initWithTitle时必须有一个标题标签,其userInteractionEnabled是YES。我仍然想知道它是如何发生的以及如何解决它,谢谢。

我使用它,它可以:D工作。希望它对你有所帮助:)

UIButton* backToRecent = [UIButton buttonWithType:UIButtonTypeCustom];
[backToRecent setImage:[UIImage imageNamed:@"navB_V.png"]forState:UIControlStateNormal];
[backToRecent setTitle:@"button title" forState:UIControlStateNormal];
[backToRecent addTarget:self action:@selector(deselect) forControlEvents:UIControlEventTouchUpInside];
[backToRecent setFrame:CGRectMake(0, 0, 45, 30)];
[backToRecent setImageEdgeInsets:UIEdgeInsetsMake(0, -30, 0, -30)];
UIBarButtonItem* navItem = [[UIBarButtonItem alloc]initWithCustomView:backToRecent];
[self.navigationItem setLeftBarButtonItem:navItem];

最新更新