简体   繁体   中英

class added dynamically doesn't respond touched in swift

I declare TestClass contain button and textField and constraint it and then add this class to view controller

import Foundation
import UIKit

class TestClass : UIView {

    let testButton : UIButton = {
        let button = UIButton()
        button.setTitle("Test", for: .normal)
        button.setTitleColor(.red ,for:.normal)
        return button
    }()

    let testInput : UITextField = {
        let input = UITextField()
        input.placeholder = "type here"
        return input
    }()


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    func setupView(){

        addSubview(testButton)
        addSubview(testInput)

        testButton.translatesAutoresizingMaskIntoConstraints = false
        testInput.translatesAutoresizingMaskIntoConstraints  = false

        testButton.topAnchor.constraint(equalTo: self.topAnchor, constant: 20).isActive = true
        testButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
        testButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
        testButton.widthAnchor.constraint(equalToConstant: 50).isActive = true

        testInput.topAnchor.constraint(equalTo: self.topAnchor, constant: 70).isActive = true
        testInput.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
        testInput.heightAnchor.constraint(equalToConstant: 30).isActive = true
        testInput.widthAnchor.constraint(equalToConstant: 100).isActive = true

    }



    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}

in the viewDidLoad I add this class and it appeared. but when I tap on textField or button it doesn't respond

class ViewControll : UIViewController {

    let newClass : TestClass = {
        let view = TestClass()
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(newClass)
        newClass.frame.size = CGSize(width: 200, height: 200)
        newClass.translatesAutoresizingMaskIntoConstraints = false
        newClass.topAnchor.constraint(equalTo: contView.topAnchor, constant: 300).isActive = true
        newClass.leadingAnchor.constraint(equalTo: contView.leadingAnchor, constant: 30).isActive = true
        newClass.testButton.addTarget(self, action: #selector(prnt), for: .touchUpInside)

    }
    @objc func prnt(){
        print("bla bla bla")
    }
}

when I click on button or tap on textField nothing happened can anyone help me please

Constrains don't define height and width of your TestClass - set frame doesn't work. You need to add constraints for this as well, for example, in viewDidLoad method:

newClass.heightAnchor.constraint(equalToConstant: 200).isActive = true
newClass.widthAnchor.constraint(equalToConstant: 200).isActive = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM