首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >UIButton上的addTarget不工作

UIButton上的addTarget不工作
EN

Stack Overflow用户
提问于 2018-06-08 19:23:54
回答 2查看 167关注 0票数 0

我有一个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)

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

按预期将该按钮添加到视图中,但单击该按钮时不会调用"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。我不确定我这样做的方式是否正确。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-11 15:50:45

我通过将自定义类中的约束设置为其superview而不是控制器,以某种方式修复了它。

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

    }
}
票数 0
EN

Stack Overflow用户

发布于 2018-06-08 19:35:17

尝试将操作更改为

action: #selector(buttonClicked(_:))

和函数

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50759592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档