UIButton上的addTarget不起作用

时间:2018-06-08 11:23:54

标签: ios swift uibutton addtarget

我有一个UIView类

class FloatingView  : UIView {
    lazy var floatingButton : UIButton = {
        let button = UIButton(type: UIButtonType.system)
        button.setBackgroundImage(#imageLiteral(resourceName: "ic_add_circle_white_36pt"), for: .normal)
        button.tintColor = UIColor().themePurple()
        button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

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

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

    func setupViews(){ addSubview(floatingButton) }

    override func didMoveToWindow() {
        super.didMoveToWindow()

        floatingButton.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor).isActive = true
        floatingButton.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
        floatingButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
        floatingButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    }

    @objc func buttonClicked (){
        print("Clicked..")
    }

添加此内容以

查看
let floatingButton = FloatingView()
view.addSubview(floatingButton)

我还指定了浮动视图的约束。

Screenshot of button

按钮已按预期添加到视图中,但单击按钮时未调用“buttonClicked”功能。单击时按钮上的淡入淡出动画正在运行。我尝试过UITapGesture但没有工作。

我已按以下方式更新课程

class FloatingView{
    lazy var floatingButton : UIButton = {
        let button = UIButton(type: UIButtonType.system)
        button.setBackgroundImage(#imageLiteral(resourceName: "ic_add_circle_white_36pt"), for: .normal)
        button.tintColor = UIColor().themePurple()
        button.addTarget(self, action: #selector(buttonClicked(_:)), for: UIControlEvents.touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    private var view : UIView!

    func add(onview: UIView ){
        view = onview
        configureSubViews()
    }

    private func configureSubViews(){
        view.addSubview(floatingButton)
        floatingButton.rightAnchor.constraint(equalTo: view.layoutMarginsGuide.rightAnchor).isActive = true
        floatingButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor).isActive = true
        floatingButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
        floatingButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    }

    @objc func buttonClicked(_ sender : UIButton){
        print("Button Clicked")
    }
}

在控制器中

let flButton = FloatingView()
flButton.add(onview: view)

我正在尝试创建一个浮动操作按钮like this。我不确定我是否采用了正确的方式。

2 个答案:

答案 0 :(得分:1)

尝试将操作更改为

action: #selector(buttonClicked(_:))

的功能

@objc func buttonClicked(_ sender: UIButton){..}

答案 1 :(得分:0)

我通过将自定义类中的约束设置为超级视图而不是从控制器来修复它。

func configureSubviews(){
    if let superView = superview {
        superView.addSubview(floatingButton)
        widthAnchor.constraint(equalToConstant: circleSpanArea).isActive = true
        heightAnchor.constraint(equalTo: widthAnchor).isActive = true
        centerXAnchor.constraint(equalTo: floatingButton.centerXAnchor).isActive = true
        centerYAnchor.constraint(equalTo: floatingButton.centerYAnchor).isActive = true

        floatingButton.rightAnchor.constraint(equalTo: superView.layoutMarginsGuide.rightAnchor).isActive = true
        floatingButton.bottomAnchor.constraint(equalTo: superView.layoutMarginsGuide.bottomAnchor).isActive = true
        floatingButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
        floatingButton.widthAnchor.constraint(equalToConstant: 60).isActive = true

    }
}