gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:25:02 24 4
gpt4 key购买 nike

我正在尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com