将UIImageView或UITextView添加到自定义UIButton类的正确方法是什么?

时间:2017-02-21 08:02:48

标签: ios swift uiimageview uibutton uitextview

我已经构建了一个自定义的UIButton类,但是我在努力添加对象而不影响按钮的行为。我已经在下面展示了这门课程。当我在主代码中使用该类并添加目标时,该按钮仅适用于图像或文本未覆盖的区域。如何添加对象并使整个按钮区域的行为方式相同?

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)

    }
}

1 个答案:

答案 0 :(得分:1)

猜测一下,希望它有效。

UIImageViewUITextView通常不允许"用户互动",这意味着用户无法点击它们并期望应用根据此做出反应。这可能就是为什么当您点击图片视图时,该事件不会传递到下面的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按钮,非常接近您制作的按钮,唯一的区别是我已将backgroundColor添加到myImageViewframe上的myTextisUserInteractionEnabled = 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!!")
    }
}

这给了我这个漂亮的UI

enter image description here

当我点击红色图像,蓝色按钮本身或" Hello" label我可以在我的控制台中看到这个:

It's alive!!
It's alive!!
It's alive!!

所以好消息是它似乎有效,现在我们只需要弄清楚你的设置和我的设置之间的区别是什么:)

希望有效并且有所帮助。