【问题标题】:How to stick UIButton to the bottom of UIScrollView? (Chatto framework)如何将 UIButton 粘贴到 UIScrollView 的底部? (Chatto 框架)
【发布时间】:2016-08-30 02:53:44
【问题描述】:

我想创建这样一个 UI,它有两个按钮粘在屏幕底部,一个 UIScrollView 在它们上方。我正在使用 Chatto 框架,如果有人可以根据 https://github.com/badoo/Chatto/tree/master/ChattoApp/ChattoApp 举例说明如何做到这一点,那就太好了。

这是我想要的视图的可视化。

【问题讨论】:

  • 您不应将按钮粘贴到滚动视图上,但应将其粘贴到 ui 视图上,并且滚动视图应位于按钮上方

标签: ios swift uiscrollview uibutton chat


【解决方案1】:

对此有更好的解决方案。您可以通过禁用相应按钮的 Auto Layout(button.translatesAutoresizingMaskIntoConstraints = false) 属性或浮动按钮的任何 UIView 来做到这一点:

Swift 4 示例

button.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
     button.rightAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.rightAnchor, constant: -10).isActive = true
     button.bottomAnchor.constraint(equalTo: tableView.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
     button.rightAnchor.constraint(equalTo: tableView.layoutMarginsGuide.rightAnchor, constant: 0).isActive = true
     button.bottomAnchor.constraint(equalTo: tableView.layoutMarginsGuide.bottomAnchor, constant: -10).isActive = true
}

【讨论】:

    【解决方案2】:

    您可以使用约束轻松做到这一点。如果您使用故事板,您可以使用它们的“Pin”和“Align”功能设置约束。如果您在代码中构建它,您将希望以编程方式设置您的约束。请务必添加所有必要的约束以完全定义视图的显示方式。

    只有一个按钮的伪示例:

    let button = UIButton()
    self.view.addsubview(button)
    // pin button to bottom of superview, 
    let buttonBottomConstraint = NSLayoutConstraint(item: button, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
    self.addConstraint(buttonBottomConstraint)
    // left of superview, 
    // right of superview, 
    // and height
    
    let scrollView = UIScrollView()
    self.view.addsubview(scrollView)
    // and bottom edge to top edge of button
    let scrollViewBottomConstraint = NSLayoutConstraint(item: scrollView, attribute: .Bottom, relatedBy: .Equal, toItem: button, attribute: .Top, multiplier: 1.0, constant: 0.0)
    self.addConstraint(scrollViewBottomConstraint)
    // left of superview, 
    // right of superview, 
    // pin scrollview to top of superview, 
    

    【讨论】:

      【解决方案3】:

      添加一个全屏滚动视图,然后在底部添加一个 UIView(锚定到屏幕底部)。然后将 2 个按钮添加到 UIView。

      如果您想让它半透明,则将 UIView 背景颜色设置为 clearColor,然后添加一个 alpha 为 0.6 的 UIView,并将其添加到按钮上方的原始 UIView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2016-04-12
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多