UIScrollView按钮未接收触摸事件

时间:2018-02-27 13:12:21

标签: ios swift uiscrollview

我在他的lets build that app.com教程中使用了自动布局和brian voong的.anchor效用函数。

我已经在UIScrollView中广泛搜索了按钮未接收触摸事件并尝试过:

1)对UIScrollView进行子类化并覆盖touchesShouldCancel

class RUIScrollView:UIScrollView {
    override func touchesShouldCancel(in view: UIView) -> Bool {
        return false
    }
}

2)在所述按钮上明确启用用户互动,将其置于最前面(我已尝试scrollview.bringSubvew(toFront:UIView)containerViewbringSubvew(toFront:UIView)view.bringSubvew(toFront:UIView)

class AboutViewController: UIViewController, UIScrollViewDelegate {
    override func viewDidLayoutSubviews() {
        scrollView.contentSize = CGSize(width: view.frame.width, height: 1500)
        scrollView.bringSubview(toFront: conferenceInfoButton)
        conferenceInfoButton.isUserInteractionEnabled = true


    }

    let scrollView:RUIScrollView = {
        let sv = RUIScrollView()
            //tried true and false for all the following properties:
        sv.alwaysBounceVertical = true
        sv.isScrollEnabled = true
        sv.delaysContentTouches = true 
        sv.canCancelContentTouches = true
        return sv
    }()

    let conferenceInfoButton: LeftAlignedIconButton = {
       let button = LeftAlignedIconButton()
       button.layer.cornerRadius = 25
       button.backgroundColor = UIColor.rutgersRed
       button.setTitle("Conference Info", for: UIControlState.normal)
       button.titleLabel?.font = UIFont(name: "Helvetica", size: 21)
       button.titleLabel?.textColor = UIColor.white
       button.contentHorizontalAlignment = .left
       button.isUserInteractionEnabled  = true
       button.setImage(UIImage(named: "logo_collaborate"), for: UIControlState.normal)
       button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
       button.imageEdgeInsets = UIEdgeInsetsMake(0, 25, 0, 0)
       return button
    }()
    ...
}

我的视图层次结构和约束:

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.backgroundColor
    scrollView.delegate = self
    view.addSubview(scrollView)

    scrollView.addSubview(containerView)

    containerView.addSubview(conferenceInfoButton)


    conferenceInfoButton.isUserInteractionEnabled = true
    self.conferenceInfoButton.addTarget(self, action: #selector(conferenceIntoButtonPressed), for: .touchUpInside)

    scrollView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: view.frame.width, height: 0)
    containerView.anchor(top:scrollView.topAnchor, left: scrollView.leftAnchor, bottom: scrollView.bottomAnchor, right: scrollView.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: scrollView.frame.width, height: 0)

conferenceInfoButton.anchor(top: locationLabel.bottomAnchor, left: nil, bottom: nil, right: nil, paddingTop: 25, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 300, height: 50)

conferenceInfoButton.centerXAnchor.constraintEqualToSystemSpacingAfter(self.scrollView.centerXAnchor,multiplier:1).isActive = true       }

@objc
func conferenceIntoButtonPressed() {
    debugPrint("hello, its me")
}

无论我对上面列出的这些设置做了什么尝试,我都无法打印此消息!

1 个答案:

答案 0 :(得分:1)

为按钮添加约束

conferenceInfoButton.anchor(top: containerView.topAnchor, left: containerView.leftAnchor, bottom: containerView.bottomAnchor, right: containerView.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: containerView.frame.width, height: 0)