swift - 如何使背景图像适合 ScrollView ?

标签 swift background uiscrollview scrollview programmatically

scaleAspectfit 似乎不起作用。我试图让我的整个背景图像稍微适合背景,看到更多的图像。我添加了一个 ScrollView ,这样用户就可以向上和向下滚动一点点来查看图像。

但是,我的应用程序将图像放大了太多。

lazy var scrollView: UIScrollView = {
    let view = UIScrollView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.contentSize.height = 800
    view.bounces = true
    return view
}() 

覆盖函数 viewDidLoad() { super.viewDidLoad()

    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
    backgroundImage.image = UIImage(named: "backgroundImage.png")
    backgroundImage.contentMode = UIViewContentMode.scaleAspectFit
    view.insertSubview(backgroundImage, at: 0)

    view.addSubview(scrollView)
    scrollView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)

    scrollView.addSubview(backgroundImage)
    backgroundImage.center(x: scrollView.centerXAnchor, y: scrollView.centerYAnchor)


 }

//如果你们想知道,这只是我用于自动布局的扩展

extension UIView {

func anchor(top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) {

    translatesAutoresizingMaskIntoConstraints = false

    if let top = top {
        topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true
    }

    if let left = left {
        leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
    }

    if let bottom = bottom {
        bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true
    }

    if let right = right {
        rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
    }

    if width != 0 {
        widthAnchor.constraint(equalToConstant: width).isActive = true
    }

    if height != 0 {
        heightAnchor.constraint(equalToConstant: height).isActive = true
    }

func center(x: NSLayoutXAxisAnchor?, y: NSLayoutYAxisAnchor? ) {

    translatesAutoresizingMaskIntoConstraints = false

    if let x = x {
        centerXAnchor.constraint(equalTo: x).isActive = true
    }

    if let y = y {
        centerYAnchor.constraint(equalTo: y).isActive = true
    }
}
}

最佳答案

首先您在此处的 2 个位置添加相同的 imageView

view.insertSubview(backgroundImage, at: 0)

这里

scrollView.addSubview(backgroundImage)

所以最终它只会被添加到 scrollView 因为它是一个对象

其次您需要像这样约束 imageView

backgroundImage.anchor(top: scrollView.topAnchor, left: scrollView.leftAnchor, bottom: scrollView.bottomAnchor, right: scrollView.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)

然后imageView会根据scrollView的内容进行拉伸(stretch)

第三 contentMode 应该是任一个

backgroundImage.contentMode =.scaleToFill // or .scaleAspectFill

应该提到您可以将 imageView 添加到 self.view 并使 scrollView 背景透明以避免因拉伸(stretch)导致 imageView 的质量和外观下降

关于swift - 如何使背景图像适合 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52281500/

相关文章:

objective-c - 防止在 Swift 中子类化 Objective-C 类

swift - 按下 Shift 键时强制小写

Android - 使背景图像不拉伸(stretch)它的分配 View

iphone - 如何为 UIScrollView 中的内容区域设置背景颜色?

iphone - UIScrollView背后如何与UIView交互

ios - 将文本水平放入 UIScrollView 和适当的 AutoLayout - Swift

Swift Firebase 从数据库显示到 TableView 中的 label.text

ios - 如何使用 Swift 启用 Google map 缩放控件

javascript - 如何使用链接更改背景图像

android - 背景图像拉伸(stretch)