【问题标题】:Buttons in UIView are not having an effect when tappedUIView 中的按钮在点击时没有效果
【发布时间】:2019-01-29 19:28:44
【问题描述】:

我在主视图控制器上使用带有按钮的视图。直到最近,当我开始处理约束以使事物在视觉上看起来更好时,我的按钮才能正常工作。调整一些尺寸和约束后,按钮停止工作。

这是我的设置: 我有一个自定义视图,我在其中设置了 3 个按钮并向它们添加了约束

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

        let buttonSize: CGFloat = 90.0

        addSubview(photoButton)
        photoButton.anchor(top: nil, leading: nil, bottom: bottomAnchor, trailing: nil, padding: .init(top: 0, left: 0, bottom: 0, right: 0), size: .init(width: buttonSize, height: buttonSize))
        photoButton.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true

        addSubview(homeButton)
        homeButton.anchor(top: nil, leading: nil, bottom: bottomAnchor, trailing: photoButton.leadingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 20), size: .init(width: buttonSize, height: buttonSize))

        addSubview(profileButton)
        profileButton.anchor(top: nil, leading: photoButton.trailingAnchor, bottom: bottomAnchor, trailing: nil, padding: .init(top: 0, left: 20, bottom: 0, right: 0), size: .init(width: buttonSize, height: buttonSize))
    }

在我的主视图控制器中,我为按钮添加了目标,以便在点击它们时调用函数。

let buttonView = HomeBottomControlsView()

在我看来确实加载了

        buttonView.homeButton.addTarget(self, action: #selector(openTrending), for: .touchUpInside)
        buttonView.profileButton.addTarget(self, action: #selector(openProfile), for: .touchUpInside)

        view.addSubview(overallStackView)
        view.addSubview(buttonView)

        overallStackView.translatesAutoresizingMaskIntoConstraints = false
        overallStackView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor)
        overallStackView.isLayoutMarginsRelativeArrangement = true
        overallStackView.layoutMargins = .init(top: 10, left: 12, bottom: 95, right: 12)

        buttonStackView.anchor(top: overallStackView.bottomAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 0), size: .init(width: 0, height: 0))

这些按钮以前可以工作,直到我开始使用自动布局约束。现在我似乎无法再让按钮成为可点击的了。

我正在阅读其他问题,由于初始帧的问题,按钮有时无法工作。是这里的问题吗?

【问题讨论】:

  • 使用视图调试器查看是否存在约束问题或超视图问题。
  • 你的overallStackView 占据了整个视图。并且您将 buttonStackView 的顶部约束设置为overallStackView 的底部锚点。那永远行不通。
  • @GaloTorresSevilla 谢谢!我现在先设置了 buttonStackView 约束,然后将 overstackview 的底部约束设置为 buttonStackView 的顶部,它又可以工作了!
  • 我会写一个答案以防你想接受它。很高兴你让它工作。

标签: ios swift uiview uibutton


【解决方案1】:

这个视图已经占据了整个view

overallStackView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: view.safeAreaLayoutGuide.bottomAnchor, trailing: view.trailingAnchor)

并且您将buttonStackView 的顶部锚点设置为overallStackView 的底部锚点。这就是框架没有正确定位的原因。您需要更改 buttonStackViewoverallStackView 的约束,使它们不会相互重叠

【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2016-05-25
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多