如何在swift中将背景图像缩放到屏幕尺寸?

Gui*_*i23 31 xcode background-image ios swift

我正在尝试使用模式图像在swift中为我的背景制作UIView图像.我的代码运行良好,除了我希望图像占据整个屏幕的事实.我的代码看起来像这样:self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage")!)

有谁知道如何使背景成为占据整个屏幕的图像,并且在出现在不同的iPhone屏幕尺寸时会缩放?

小智 38

注意:

我从我的旧帐户发布了这个答案(我不赞成使用,我再也无法访问了),这是我改进的答案.


您可以以编程方式执行此操作,而不是在每个视图中创建IBOutlet.只需创建一个UIView 扩展(文件 - >新建 - >文件 - > Swift文件 - >将其命名为您想要的任何内容)并添加:

extension UIView {
func addBackground() {
    // screen width and height:
    let width = UIScreen.mainScreen().bounds.size.width
    let height = UIScreen.mainScreen().bounds.size.height

    let imageViewBackground = UIImageView(frame: CGRectMake(0, 0, width, height))
    imageViewBackground.image = UIImage(named: "YOUR IMAGE NAME GOES HERE")

    // you can change the content mode:
    imageViewBackground.contentMode = UIViewContentMode.ScaleAspectFill

    self.addSubview(imageViewBackground)
    self.sendSubviewToBack(imageViewBackground)
}}
Run Code Online (Sandbox Code Playgroud)

现在,您可以将此方法用于您的视图,例如:

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.addBackground()
}
Run Code Online (Sandbox Code Playgroud)

  • 让imageViewBackground = UIImageView(frame:UIScreen.mainScreen().bounds) (2认同)

Leo*_*bus 26

只需将UIImageView添加到居中位置,并将所有边缘对齐到边缘.将其保留在那里并单击右下角,如下所示,现在继续向顶部,底部,左侧和右侧边缘添加4个约束.

在此输入图像描述

现在只需选择您的图像视图,然后使用IB检查器选择您想要的图像:填充或合适,如下所示:

在此输入图像描述


Ahm*_*d F 15

这是我之前的更新答案.

与我之前的答案相同,您可以创建UIView的扩展addBackground()并向其添加方法,如下所示:

请记住:如果要将其添加到新的.swift文件中,请记住添加 import UIKit

extension UIView {
    func addBackground(imageName: String = "YOUR DEFAULT IMAGE NAME", contentMode: UIView.ContentMode = .scaleToFill) {
        // setup the UIImageView
        let backgroundImageView = UIImageView(frame: UIScreen.main.bounds)
        backgroundImageView.image = UIImage(named: imageName)
        backgroundImageView.contentMode = contentMode
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false

        addSubview(backgroundImageView)
        sendSubviewToBack(backgroundImageView)

        // adding NSLayoutConstraints
        let leadingConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0.0)
        let trailingConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0.0)
        let topConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0)
        let bottomConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0)

        NSLayoutConstraint.activate([leadingConstraint, trailingConstraint, topConstraint, bottomConstraint])
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,此答案的更新如下:

  • Swift 4代码
  • 添加-programatically- NSLayoutConstraints:这是因为在应用我之前的答案中提到的内容时,它适用于当前的设备方向,但是当应用程序同时支持纵向/横向模式时,如果设备方向已更改,则不适用,背景图像查看大小将相同(相同大小)并且不适应设备屏幕的新宽度/高度,因此添加约束应该可以解决此问题.
  • 添加默认参数:为了获得更大的灵活性,您可能 - 有时候 - 想要更改默认图像甚至是背景的上下文模式:

用法:

假设您想要调用它viewDidLoad():

override func viewDidLoad() {
    //...

    // you can call 4 versions of addBackground() method

    // 1- this will add it with the default imageName and default contextMode
    view.addBackground()

    // 2- this will add it with the edited imageName and default contextMode
    view.addBackground(imageName: "NEW IMAGE NAME")

    // 3- this will add it with the default imageName and edited contextMode
    view.addBackground(contentMode: .scaleAspectFit)

    // 4- this will add it with the default imageName and edited contextMode
    view.addBackground(imageName: "NEW IMAGE NAME", contextMode: .scaleAspectFit)
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*son 1

这使用PureLayout。您可以只使用 AutoLayout 多几行。

UIImageView* imgView = UIImageView(image: myUIImage)
imgView.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(imgView)
self.view.addConstraints(imgView.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsetsMake(0,0,0,0))
Run Code Online (Sandbox Code Playgroud)