Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.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
Swift3 Swift 3如何使背景图像填充整个视图?_Swift3 - Fatal编程技术网

Swift3 Swift 3如何使背景图像填充整个视图?

Swift3 Swift 3如何使背景图像填充整个视图?,swift3,Swift3,我创建了一个UIView,并使用以下代码将背景图像更改为它 slot1_view?.backgroundColor = UIColor(patternImage: UIImage(named: "drum_cut.png")!) 它在iphone上运行良好,但在ipad上,背景图像会重复出现 如何将其内容模式更改为填充整个UIView的内容 请帮忙 试试这个 let backgroundImage = UIImageView(frame: UIScreen.main.bounds) backg

我创建了一个UIView,并使用以下代码将背景图像更改为它

slot1_view?.backgroundColor = UIColor(patternImage: UIImage(named: "drum_cut.png")!)
它在iphone上运行良好,但在ipad上,背景图像会重复出现

如何将其内容模式更改为填充整个UIView的内容

请帮忙

试试这个

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "RubberMat")
backgroundImage.contentMode =  UIViewContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
试试这个代码-

重复:

var img = UIImage(named: "bg.png")
view.backgroundColor = UIColor(patternImage: img!)
拉伸

var img = UIImage(named: "bg.png")
view.layer.contents = img?.cgImage
希望有帮助

Swift 5 这项工作可以很好地查看背景图像

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
 backgroundImage.image = AppImageAssets.appAssets.appBackgroundImage()
 backgroundImage.contentMode =  UIView.ContentMode.scaleAspectFill
 self.view.insertSubview(backgroundImage, at: 0)

如果您使用的是Swift5,请在按钮单击事件上尝试此操作。您可能需要为UIView设置一些布局边距

        myView?.contentMode = UIView.ContentMode.scaleToFill
        myView.layer.contents = UIImage(named:"bg.png")?.cgImage


         self.view.bringSubviewToFront(myView)

将UIImageView作为其子视图添加到视图中,然后改为在UIImageView上设置图像。因此没有非重复背景图像的选项,对吗?发布图像。UIColor-图案图像用于创建重复背景以节省内存。但是你希望它“填充”的方式意味着你实际上有一个图像,因此你应该将它作为一个(而不是一种颜色)来处理@Pochi非常感谢,我将尝试使用UIImage