这里是一个猜测,希望它有效。
UIImageView 和UITextView 通常不允许“用户交互”,这意味着用户不能点击它们并期望应用程序基于此做出反应。这可能就是为什么当您点击图像视图时,该事件没有传递到下面的UIButton。
幸运的是,修复很容易。您可以将布尔属性isUserInteractionEnabled 设置为true,您应该可以再次营业。
所以在你的情况下:
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
let myImage = UIImage(named: "AnImage")
myImageView = UIImageView(image: myImage)
myImageView.frame = CGRect(x: 10, y: 10, width: (myImage?.size.width)!, height: (myImage?.size.height)!)
myImageView.isUserInteractionEnabled = true
addSubview(myImageView)
myText = UITextView()
myText.font = UIFont(name: "Courier", size: 12)!
myText.isEditable = false
myText.isUserInteractionEnabled = true
addSubview(myText)
}
更新(在您发表评论后)
好的,所以我只是尝试使用您的按钮创建一个快速项目......它似乎工作,我可以点击 imageView,我可以点击标签,仍然从我的函数中得到答案,所以必须在我们的设置方式上有所不同。
这是我的MyButton 按钮,与您所做的非常接近,唯一的区别是我在myImageView 上添加了一个backgroundColor,在myText 和@987654333 上添加了一个frame @上myText
class MyButton: UIButton {
var myText: UITextView!
var myImageView: UIImageView!
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor.blue
let myImage = UIImage(named: "AnImage")
myImageView = UIImageView(image: myImage)
myImageView.frame = CGRect(x: 10, y: 10, width: (myImage?.size.width)!, height: (myImage?.size.height)!)
myImageView.backgroundColor = UIColor.red
addSubview(myImageView)
myText = UITextView()
myText.font = UIFont(name: "Courier", size: 12)!
myText.frame = CGRect(x: 10, y: 80, width: 100, height: 30)
myText.isEditable = false
myText.isUserInteractionEnabled = false
addSubview(myText)
}
}
这是我使用按钮的ViewController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = MyButton(frame: CGRect(x: 10, y: 100, width: 200, height: 200))
button.myText.text = "Hello"
button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
view.addSubview(button)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func didTapButton(_ sender: MyButton) {
print("It's alive!!")
}
}
这给了我这个漂亮的用户界面
当我点击红色图像、蓝色按钮本身或“Hello”标签时,我可以在控制台中看到:
It's alive!!
It's alive!!
It's alive!!
好消息是它似乎有效,现在我们只需要弄清楚你的设置和我的设置之间的区别是什么:)
希望有效并有所帮助。