调整图像的高度以适合屏幕(保持宽高比)并使其余部分以编程方式水平滚动
Resize height of image to fit screen (keep aspect ratio) and make the rest horizontally scrollable programmatically
我想让图像的高度适合屏幕的高度,但保持纵横比并使其余部分可水平滚动。
您可以查看下面的代码示例。我设法使图像的高度适合屏幕的高度,同时保持这样的纵横比:。
我的最后一个问题是 scrollView 没有在整个水平距离上滚动。只是滚动视图用于滚动较小图像尺寸的距离,所以我也尝试在像这样 How do I auto size a UIScrollView to fit its content 设置 contentView 后重置滚动视图,但不幸的是它不起作用。所以我看不到图像的边缘,但是如果我将滚动视图拉到一侧并保持 "finger" 打开,我可以看到图像。但是我一松开它就会弹回图像的中间部分。
我也看了这个:https://developer.apple.com/library/archive/technotes/tn2154/_index.html
但是我找不到解决问题的方法。
我也已经尝试过计算手动适应屏幕后图像的比例变化,而不是设置滚动视图的内容大小,但这也没有用(就像这个问题的答案: and here: )
代码如下:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = .red
return scrollView
}()
var contentView: UIView = {
let contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
return contentView
}()
var imageView: UIImageView = {
let imageView = UIImageView()
let image = UIImage(named: "Erlebnispark.jpg")
imageView.image = image
imageView.backgroundColor = .red
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(imageView)
scrollView.delegate = self
setupLayout()
}
func setupLayout() {
[
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
//contentView.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
contentView.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),
// contentView.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
// imageView.heightAnchor.constraint(equalTo: contentView.heightAnchor),
].forEach {[=10=].isActive = true}
}
}
也许有人可以帮助我。
只有一些变化...
var imageView: UIImageView = {
let imageView = UIImageView()
let image = UIImage(named: "Erlebnispark.jpg")
imageView.image = image
imageView.backgroundColor = .red
imageView.translatesAutoresizingMaskIntoConstraints = false
// use scaleToFill
//imageView.contentMode = .scaleAspectFill
imageView.contentMode = .scaleToFill
return imageView
}()
setupLayout()
中的第一行:
func setupLayout() {
// add this line
guard let img = imageView.image else { return }
约束数组的最后一行:
// add this line
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: img.size.width / img.size.height),
].forEach {[=12=].isActive = true}
您完成的约束现在告诉图像视图的 宽度 是它自己的 高度,乘数为 img.size.width / img.size.height
其他一切都很好。
我想让图像的高度适合屏幕的高度,但保持纵横比并使其余部分可水平滚动。
您可以查看下面的代码示例。我设法使图像的高度适合屏幕的高度,同时保持这样的纵横比:
我的最后一个问题是 scrollView 没有在整个水平距离上滚动。只是滚动视图用于滚动较小图像尺寸的距离,所以我也尝试在像这样 How do I auto size a UIScrollView to fit its content 设置 contentView 后重置滚动视图,但不幸的是它不起作用。所以我看不到图像的边缘,但是如果我将滚动视图拉到一侧并保持 "finger" 打开,我可以看到图像。但是我一松开它就会弹回图像的中间部分。
我也看了这个:https://developer.apple.com/library/archive/technotes/tn2154/_index.html 但是我找不到解决问题的方法。
我也已经尝试过计算手动适应屏幕后图像的比例变化,而不是设置滚动视图的内容大小,但这也没有用(就像这个问题的答案:
代码如下:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = .red
return scrollView
}()
var contentView: UIView = {
let contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
return contentView
}()
var imageView: UIImageView = {
let imageView = UIImageView()
let image = UIImage(named: "Erlebnispark.jpg")
imageView.image = image
imageView.backgroundColor = .red
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
return imageView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(imageView)
scrollView.delegate = self
setupLayout()
}
func setupLayout() {
[
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
//contentView.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
contentView.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),
// contentView.widthAnchor.constraint(equalTo: scrollView.contentLayoutGuide.widthAnchor),
imageView.topAnchor.constraint(equalTo: contentView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
// imageView.heightAnchor.constraint(equalTo: contentView.heightAnchor),
].forEach {[=10=].isActive = true}
}
}
也许有人可以帮助我。
只有一些变化...
var imageView: UIImageView = {
let imageView = UIImageView()
let image = UIImage(named: "Erlebnispark.jpg")
imageView.image = image
imageView.backgroundColor = .red
imageView.translatesAutoresizingMaskIntoConstraints = false
// use scaleToFill
//imageView.contentMode = .scaleAspectFill
imageView.contentMode = .scaleToFill
return imageView
}()
setupLayout()
中的第一行:
func setupLayout() {
// add this line
guard let img = imageView.image else { return }
约束数组的最后一行:
// add this line
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: img.size.width / img.size.height),
].forEach {[=12=].isActive = true}
您完成的约束现在告诉图像视图的 宽度 是它自己的 高度,乘数为 img.size.width / img.size.height
其他一切都很好。