Ios 什么';将UIImageView或UITextView添加到自定义UIButton类的正确方法是什么?
我已经构建了一个自定义UIButton类,但是我很难在不影响按钮行为的情况下添加对象。我已经在下面展示了这个类。当我在主代码中使用该类并添加目标时,按钮仅在图像或文本未覆盖的区域工作。如何添加对象并使整个按钮区域以相同的方式工作Ios 什么';将UIImageView或UITextView添加到自定义UIButton类的正确方法是什么?,ios,swift,uiimageview,uibutton,uitextview,Ios,Swift,Uiimageview,Uibutton,Uitextview,我已经构建了一个自定义UIButton类,但是我很难在不影响按钮行为的情况下添加对象。我已经在下面展示了这个类。当我在主代码中使用该类并添加目标时,按钮仅在图像或文本未覆盖的区域工作。如何添加对象并使整个按钮区域以相同的方式工作 class TestButton: UIButton { var myText: UITextView! var myImageView: UIImageView! required init?(coder aDecoder: NSCoder)
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
通常不允许“用户交互”,这意味着用户不能点击它们并期望应用程序基于此做出反应。这可能就是为什么当您点击图像视图时,该事件不会传递到下面的ui按钮
幸运的是,修复很容易。您可以将布尔属性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
上添加了frame
和isUserInteractionEnabled=false
在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!!
好消息是,它似乎可以工作,现在我们只需要找出您的设置和我的设置之间的区别:)
希望这能起作用并对您有所帮助。您可以禁用imageview和textview的用户交互功能,以便单击按钮。或者可以覆盖hittest方法。您可以简单地将UIButton放在任何需要的地方,并关闭其他组件的交互,而不是将组件添加到UIButton中,将其他组件仅保留在外部。感谢您回来,但不幸的是,它不起作用。我试着将UIImageView和UIExtView放在UIButton的顶部,在UIButton的外部,但这两个视图阻碍了按钮。你知道我如何改变物体的顺序吗?我已经尝试过“让myButton=UIButton()”“myButton.bringSubview(toFront:self.view!)”按钮目标在前面时仍然没有被调用。