我已经构建了一个自定义 UIButton 类,但我很难在不影响按钮行为的情况下添加对象。我已经展示了下面的类(class)。当我在主代码中使用该类并添加目标时,该按钮仅在图像或文本未覆盖的区域有效。如何添加对象并使整个按钮区域以相同的方式运行?
class TestButton: 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)!)
addSubview(myImageView)
myText = UITextView()
myText.font = UIFont(name: "Courier", size: 12)!
myText.isEditable = false
addSubview(myText)
}
}
最佳答案
这里是一个猜测,希望它有效。
UIImageView
和 UITextView
通常不允许“用户交互”,这意味着用户无法点击它们并期望应用程序基于此使用react。这可能就是为什么当您点击 ImageView 时,该事件不会传递到下面的 UIButton
。
幸运的是,修复很容易。您可以将 bool 属性 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
按钮,非常接近您制作的按钮,唯一的区别是我添加了一个 backgroundColor
到 myImageView
,myText
上的 frame
和 myText
isUserInteractionEnabled = false
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!!")
}
}
这给了我这个漂亮的 UI
当我点击红色图像、蓝色按钮本身或“你好”标签时,我可以在我的控制台中看到:
It's alive!!
It's alive!!
It's alive!!
所以好消息是它似乎有效,现在我们只需要弄清楚你的设置和我的设置之间的区别是什么:)
希望有用并有所帮助。
关于ios - 将 UIImageView 或 UITextView 添加到自定义 UIButton 类的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42361712/