用于 ScrollView 和粘性页脚的 iOS Autolayout

标签 ios autolayout ios-autolayout

假设我的布局如下图所示: enter image description here Root View - 是 viewController 的 View 。我希望我的页脚文本是

  • 如果内容适合一个屏幕(主要文本很小),则粘在屏幕底部
  • 在其他情况下距正文 15pt,距屏幕底部 15pt

我知道,我可以计算主要文本高度并将其与当前屏幕尺寸进行比较,但我想找到另一种方法(理想情况下只有在有限制的情况下)。

这可能吗?

最佳答案

要实现您正在尝试做的事情,您需要设置以下约束:

ScrollView :
- topleadingtrailingbottom 等于 RootViewtopleadingtrailingbottom

包装 View :
- topleadingtrailingbottom 等于 ScrollViewtopleadingtrailingbottom
- width 等于 ScrollView 的 width
- height greaterThanOrEqual to ScrollView 的 height

TextView :
- top, leadingtrailing 等于 WrapperViewtop, leadingtrailing

页脚 View :
- leadingtrailing 等于WrapperViewleadingtrailing
- bottom 等于 WrapperViewbottom(常量为 15)
- top greaterThanOrEqual to TextView's bottom (常量为 15)

关键是使用 greaterThanOrEqual 关系的两个约束:WrapperView 至少与 ScrollView 一样高>FooterView 的顶部距离 TextView 的底部至少有 15 的距离。

以下是使用 Storyboard时的限制条件:

enter image description here

这就是您以编程方式(使用 SnapKit)执行此操作的方式:

import UIKit
import SnapKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let scrollView = UIScrollView()
        view.addSubview(scrollView)

        let wrapperView = UIView()
        wrapperView.backgroundColor = .cyan
        scrollView.addSubview(wrapperView)

        let textView = UILabel()
        textView.numberOfLines = 0
        textView.font = UIFont.systemFont(ofSize: 30)
        textView.text = "You think water moves fast? You should see ice. It moves like it has a mind. Like it knows it killed the world once and got a taste for murder. After the avalanche, it took us a week to climb out. Now, I don't know exactly when we turned on each other, but I know that seven of us survived the slide... and only five made it out. Now we took an oath, that I'm breaking now. We said we'd say it was the snow that killed the other two, but it wasn't. Nature is lethal but it doesn't hold a candle to man."
        //textView.text = "You think water moves fast? You should see ice. It moves like it has a mind."
        textView.backgroundColor = .yellow
        wrapperView.addSubview(textView)

        let footer = UILabel()
        footer.textAlignment = .center
        footer.text = "Footer text"
        footer.backgroundColor = .orange
        wrapperView.addSubview(footer)

        scrollView.snp.makeConstraints { (make) in
             make.edges.equalTo(view)
        }

        wrapperView.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
            make.width.equalTo(scrollView)
            make.height.greaterThanOrEqualTo(scrollView)
        }
        textView.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(0)
        }
        footer.snp.makeConstraints { (make) in
            make.top.greaterThanOrEqualTo(textView.snp.bottom).offset(15)
            make.left.right.equalTo(0)
            make.bottom.equalTo(-15)
        }
    }
}

长文本截图:

enter image description here

带有短文本的屏幕截图:

enter image description here

关于用于 ScrollView 和粘性页脚的 iOS Autolayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40611395/

相关文章:

iOS 在弹出窗口中显示 View Controller

ios - 在swift 3中获取推送数据

iphone - 在运行时自动调整 UIButton 的大小。

ios - ios中如何根据方向改变图像位置?

ios - iOS 中自动布局和自动调整大小的基本区别是什么

ios - 自动布局特征集合更改时的通知(不是方法)?

ios - 变量 NSString anos 使用 NSLocalizedString Xcode

ios - PushViewController 后跟 'back' 按钮有时不会弹出 View

ios - 动画 View 在启动时向上移动

ios - UIView 的动画约束(作为主视图的 subview )