如何将 UIButton 粘贴到 UIScrollView 的底部? (Chatto 框架)
How to stick UIButton to the bottom of UIScrollView? (Chatto framework)
我想创建这样一个 UI,它有两个按钮贴在屏幕底部,UIScrollView 位于它们上方。我正在使用 Chatto 框架,如果有人能给我一个如何基于 https://github.com/badoo/Chatto/tree/master/ChattoApp/ChattoApp.
的例子,那就太好了
这是我想要的可视化视图。
您可以使用约束轻松地做到这一点。如果您使用的是故事板,则可以使用它们的 "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,
添加一个全屏滚动视图,然后在底部添加一个 UIView(锚定到屏幕底部)。然后将 2 个按钮添加到 UIView。
如果你想让它半透明,然后将 UIView 背景颜色设为 clearColor,然后添加一个 alpha 为 0.6 的 UIView,并将其添加到按钮上方的原始 UIView。
对此有更好的解决方案。您可以通过禁用相应按钮的自动布局(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
}
我想创建这样一个 UI,它有两个按钮贴在屏幕底部,UIScrollView 位于它们上方。我正在使用 Chatto 框架,如果有人能给我一个如何基于 https://github.com/badoo/Chatto/tree/master/ChattoApp/ChattoApp.
的例子,那就太好了这是我想要的可视化视图。
您可以使用约束轻松地做到这一点。如果您使用的是故事板,则可以使用它们的 "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,
添加一个全屏滚动视图,然后在底部添加一个 UIView(锚定到屏幕底部)。然后将 2 个按钮添加到 UIView。
如果你想让它半透明,然后将 UIView 背景颜色设为 clearColor,然后添加一个 alpha 为 0.6 的 UIView,并将其添加到按钮上方的原始 UIView。
对此有更好的解决方案。您可以通过禁用相应按钮的自动布局(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
}