ios - 如何以编程方式将 ImageView 与 ScrollView 的底部对齐?

标签 ios swift uiscrollview uiimageview ios-autolayout

我正在尝试使用 Autolayout 以编程方式将背景图像对齐到适合屏幕的 ScrollView 的底部。理想情况下,我希望图像始终与 ScrollView 的底部对齐。当 ScrollView 的内容超出屏幕高度或 ScrollView 内容大小小于屏幕高度且 ScrollView 适合整个屏幕时。

我的 View

class MyView: UIView {

    let myScrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.bounces = false
        return scrollView
    }()

    let contentView: UIView = {
        let view = UIView()
        view.backgroundColor = .red
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    let myLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        label.text = "Hello world"
        label.font = UIFont.systemFont(ofSize: 24)
        return label
    }()

    let myImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = #imageLiteral(resourceName: "Mask Group 3")
        return imageView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
        setupConstraints()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupView() {
        backgroundColor = .white

        addSubview(myScrollView)

        myScrollView.addSubview(contentView)

        contentView.addSubview(myLabel)
        contentView.addSubview(myImageView)
    }

    private func setupConstraints() {
        myScrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
        myScrollView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        myScrollView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
        myScrollView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true

        contentView.topAnchor.constraint(equalTo: myScrollView.topAnchor).isActive = true
        contentView.bottomAnchor.constraint(equalTo: myScrollView.bottomAnchor).isActive = true
        contentView.leftAnchor.constraint(equalTo: myScrollView.leftAnchor).isActive = true
        contentView.rightAnchor.constraint(equalTo: myScrollView.rightAnchor).isActive = true
        contentView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
        // If I am setting this and when the content size go beyond the screen, it does not scroll
        // If I don't set this, there is no content size and image view will not position correctly
//        contentView.heightAnchor.constraint(equalTo: heightAnchor).isActive = true

        myLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 200).isActive = true
        myLabel.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true

        myImageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
        myImageView.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
        myImageView.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
    }

}

MyViewController

import UIKit

class MyViewController: UIViewController {

    override func loadView() {
        view = MyView()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: false)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

}

插图

ScrollView

最佳答案

我找到了解决方案。

  1. 需要一个contentView
  2. contentView 的上、左、下、右约束等于 设置为scrollView 边缘。
  3. 设置contentView的宽度等于view的宽度 anchor
  4. 设置contentView的高度 greaterThanOrEqualTo view的高度 anchor
  5. imageView 的底部 equal 设置为 contentView 的底部 anchor 。
  6. 对于 imageView 的顶部,将约束设置为具有 greaterThanOrEqualTo 的元素,以为其提供恒定的间隙并避免在较小的屏幕中元素重叠。<

关于ios - 如何以编程方式将 ImageView 与 ScrollView 的底部对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56832330/

相关文章:

ios - 键盘显示时 Tableview 滚动内容

ios - 在 UIScrollView 中旋转 UIImageView

iphone - 如何使用计时器自动滚动 UIScrollView?

ios - 关闭 ViewController 后,我的 View 保持在 iOS 7 中以前的 View Controller 方向

ios - 从NSMutableArray删除空NSString的最快方法是什么?

ios - 在 Swift 中找不到源文件

ios - 从各种标签中获取值并进行简单计算

iphone - TableView 细胞图像视网膜问题

ios - 动态更改 iOS 文本字段以显示货币

swift - NSTextField:如何检测字符串何时太长?