这里是代码
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];