我有一个包含 UIButton
的 inputAccessoryView
,但是 UIButton
没有响应点击(甚至没有改变它的图像)。我不确定为什么。这是我相当简单的代码:
UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
UIButton *selectionModeOkButton = [[UIButton alloc] initWithFrame:CGRectMake(264, 11, 50, 46)];
[selectionModeOkButton setImage:[UIImage imageNamed:@"btn_count_ok_unpressed"] forState:UIControlStateNormal];
[selectionModeOkButton setImage:[UIImage imageNamed:@"btn_count_ok_pressed"] forState:UIControlStateHighlighted];
[selectionModeOkButton addTarget:self action:@selector(selectionModeOkButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[accessoryView addSubview:selectionModeOkButton];
self.selectionModeTextField.inputAccessoryView = accessoryView;
最佳答案
来自Apple docs for UIImageView
:
A Boolean value that determines whether user events are ignored and removed from the event queue.
This property is inherited from the UIView parent class. This class changes the default value of this property to NO.
这是在告诉您,默认情况下,新的 UIImageView
将 userInteractionEnabled
设置为 NO
。所以,添加这一行:
accessoryView.userInteractionEnabled = YES;
有关 userInteractionEnabled
究竟如何工作的更多信息,请查看 Apple docs for UIView
中该属性的条目(继承该属性的类)。
关于ios - 添加到 inputAccessoryView 的 UIButton 未检测到触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467823/