【问题标题】:Bring UIButton above UIScrollview将 UIButton 带到 UIScrollview 上方
【发布时间】:2020-05-31 08:56:06
【问题描述】:

我想使用 uiscrollview 在 UIViewController 内制作一个浮动按钮。我之前尝试使用 xib 并且它可以工作,但是当我选择项目时某些功能不起作用。但是当我使用 ui 代码时,它可以选择,问题是我尝试将按钮放在 uiscrollview 上方,就像我在 xib 文件中所做的那样。但它没有出现,请帮助。这是我的代码设置。如果您需要查看其他 uicode,请告诉我。谢谢

    private func setupScrollView() {
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.contentSize.height = 2000

        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
    }

    private func setupAbsenceBtn() {
        view.insertSubview(startAbsenceBtn, aboveSubview: scrollView)

        NSLayoutConstraint.activate([
            startAbsenceBtn.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            startAbsenceBtn.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            startAbsenceBtn.heightAnchor.constraint(equalToConstant: 55),
            startAbsenceBtn.widthAnchor.constraint(equalToConstant: 307)
        ])

    }

【问题讨论】:

  • 在这种情况下,图像可能是 heloful。
  • 尝试在scrollView 中添加并使用bringSubviewToFront 作为scrollView.addSubView(startAbsenceBtn) 然后startAbsenceBtn.superview?.bringSubviewToFront(startAbsenceBtn)
  • 将按钮添加到滚动视图,但将其限制为滚动视图的frameLayoutGuide,而不是contentLayoutGuide。然后它将在其余内容滚动时保持固定

标签: ios swift uiscrollview uibutton


【解决方案1】:

startAbsenceBtn.translatesAutoresizingMaskIntoConstraints = false 不见了。

【讨论】:

  • 我的 startAbsenceBtn 是一个具有默认自动布局的自定义按钮。所以没有必要添加一个,我也尝试了你的建议,但它仍然不起作用。
  • 能否给出scrollView和button的frame位置的输出。
  • 我尝试从 frame.origin 打印 scrollview 和 btn 都有返回值 (0.0, 0.0) (0.0, 0.0) @AbhirajKumar
  • 它对我来说很好。你有没有调用``` setupScrollView() setupAbsenceBtn()```。问题只能在这种情况下重现。
  • 是的,但我要为你竖起大拇指。因为我决定使用xib。谢谢@AbhirajKumar
猜你喜欢
  • 2010-12-27
  • 2012-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-02
相关资源
最近更新 更多