Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用swift在Xcode中设置背景图像?_Ios_Swift_Background - Fatal编程技术网

Ios 如何使用swift在Xcode中设置背景图像?

Ios 如何使用swift在Xcode中设置背景图像?,ios,swift,background,Ios,Swift,Background,如何使用swift为Xcode 6中的主视图控制器设置背景图像?我知道您可以在助理编辑中这样做,如下所示: override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.yellowColor() } 为我的资产文件夹中的图像设置背景的代码是什么 override func viewDidLoad() { super.viewDidLoad() self.view

如何使用swift为Xcode 6中的主视图控制器设置背景图像?我知道您可以在助理编辑中这样做,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor.yellowColor()
}
为我的资产文件夹中的图像设置背景的代码是什么

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))
}
更新日期:2020年5月20日:

旋转设备后,上面的代码片段无法正常工作。下面是一个解决方案,它可以使图像在旋转后根据屏幕大小拉伸:

class ViewController: UIViewController {

    var imageView: UIImageView = {
        let imageView = UIImageView(frame: .zero)
        imageView.image = UIImage(named: "bg_image")
        imageView.contentMode = .scaleToFill
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.insertSubview(imageView, at: 0)
        NSLayoutConstraint.activate([
            imageView.topAnchor.constraint(equalTo: view.topAnchor),
            imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

我是iOS开发的初学者,所以我想分享我在这一节中得到的全部信息

首先从image assets images.xAssets创建图像集

根据这里是所有大小需要创建背景图像

For iPhone 5:
   640 x 1136

   For iPhone 6:
   750 x 1334 (@2x) for portrait
   1334 x 750 (@2x) for landscape

   For iPhone 6 Plus:
   1242 x 2208 (@3x) for portrait
   2208 x 1242 (@3x) for landscape

   iPhone 4s (@2x)      
   640 x 960

   iPad and iPad mini (@2x) 
   1536 x 2048 (portrait)
   2048 x 1536 (landscape)

   iPad 2 and iPad mini (@1x)
   768 x 1024 (portrait)
   1024 x 768 (landscape)

   iPad Pro (@2x)
   2048 x 2732 (portrait)
   2732 x 2048 (landscape)
调用图像背景我们可以使用以下方法从图像资产调用图像UIImageName:background这里是完整的代码示例

  override func viewDidLoad() {
          super.viewDidLoad()
             assignbackground()
            // Do any additional setup after loading the view.
        }

  func assignbackground(){
        let background = UIImage(named: "background")

        var imageView : UIImageView!
        imageView = UIImageView(frame: view.bounds)
        imageView.contentMode =  UIViewContentMode.ScaleAspectFill
        imageView.clipsToBounds = true
        imageView.image = background
        imageView.center = view.center
        view.addSubview(imageView)
        self.view.sendSubviewToBack(imageView)
    }

你也可以试试这个,它实际上结合了其他海报上的答案:

    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
    backgroundImage.image = UIImage(named: "RubberMat")
    backgroundImage.contentMode =  UIViewContentMode.scaleAspectFill
    self.view.insertSubview(backgroundImage, at: 0)
斯威夫特4

斯威夫特4号

swift 4中API的背景图像,包括:

用法:

someview.addBackgroundImage(imgUrl: "yourImgUrl", placeHolder: "placeHolderName")
XCode 12.2游乐场中的Swift 5.3

将光标放在早期版本中插入文字所在的代码中的任何位置。从顶部菜单编辑器中选择->插入图像文字。。。并导航到该文件。单击“打开”

该文件将被添加到游乐场资源文件夹中,然后在游乐场运行时,无论选择哪个视图,都会显示该文件

如果某个文件已经在游乐场捆绑包中,例如在/Resources中,则可以将其直接拖动到代码中所需的位置,并在该位置用图标表示


请参阅TableViewController的SWIFT 5

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

tableView.backgroundView = UIImageView(image:"image.jpg")
tableView.backgroundView?.contentMode = .scaleToFill
}

你在找什么?更多关于UIView背景图片的精彩解答!谢谢如何在.xcsets中的一个图像集下设置不同的图像大小?工作非常完美!这对我来说是可行的,但是从2017年起,UIScreen.mainScreen.bounds现在是UIScreen.main.bounds和self.view.insertSubviewbackgroundImage,atIndex:0是self.view.insertSubviewbackgroundImage,在:0.scaleToFill将通过旋转使图像严重失真。如果你把一个梯度或者什么东西放在纵横比不重要的地方,那就好了,但你的初始答案实际上是大多数人可能想要的。除此之外,结合ScaleSpectFill和对视图的约束,这是正确的答案。请注意,严格来说,不需要插入图像视图而不是addSubview,除非无序添加视图。在每个子视图层的顶部添加子视图,所以只要背景是第一个,它就会是背景。我尝试将它与背景图像一起使用。在这个解决方案中存在一个问题,因为它把图像放在一个重复的模式中。将图像设置为背景颜色是我认为非常混乱的事情。虽然它可能会起作用,但我强烈建议采用另一种方法,只需在其他内容下方添加一个图像视图,与大多数其他答案类似,这是最接近正确的方法。只有一行+++对我来说是完美的。Thankshow是否使用缩放aspectFill调整大小?SuiViewContentMode已重命名为UIView.ContentMode
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "bg_name.png")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
import UIKit
import Kingfisher

extension UIView {

func addBackgroundImage(imgUrl: String, placeHolder: String){
    let backgroundImage = UIImageView(frame: self.bounds)
    backgroundImage.kf.setImage(with: URL(string: imgUrl), placeholder: UIImage(named: placeHolder))
    backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
    self.insertSubview(backgroundImage, at: 0)

}
}
someview.addBackgroundImage(imgUrl: "yourImgUrl", placeHolder: "placeHolderName")
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)

tableView.backgroundView = UIImageView(image:"image.jpg")
tableView.backgroundView?.contentMode = .scaleToFill
}