ios - 添加到 inputAccessoryView 的 UIButton 未检测到触摸

标签 ios objective-c xcode uibutton inputaccessoryview

我有一个包含 UIButtoninputAccessoryView,但是 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.

这是在告诉您,默认情况下,新的 UIImageViewuserInteractionEnabled 设置为 NO。所以,添加这一行:

accessoryView.userInteractionEnabled = YES;

有关 userInteractionEnabled 究竟如何工作的更多信息,请查看 Apple docs for UIView 中该属性的条目(继承该属性的类)。

关于ios - 添加到 inputAccessoryView 的 UIButton 未检测到触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26467823/

相关文章:

ios - Xcode Image Assets 为视网膜设备返回错误的图像

xcode - Xcode 中的 "Header Search Paths"与 "User Header Search Paths"对比?

iphone - 使用启动画面而不是加载画面——是否违反了 Apple 的 HIG?

ios - 根据 URL 更改 UIImageView

objective-c - 在MetalKit应用程序中处理输入事件

ios - 我们可以在类扩展中添加继承语法吗

objective-c - 如果用户的 Facebook session 有效,如何重定向到另一个 View Controller ?

objective-c - NSTableview 不会在 Core Data 应用程序中排序

xcode - @import 导致解析问题 : "could not build module"

php - 确保 http(s) 请求来 self 的 iOS 应用