如何在swift 4中填充子视图的背景图像?

qyuhtwio  于 2023-02-21  发布在  Swift
关注(0)|答案(4)|浏览(144)

我用过滚动视图。在滚动视图上我有一个UIView,我可以在那里设置我的图像作为背景。但问题是,图像高度没有与UIView高度合并。它是在滚动底部时显示的。我想添加整个UIView的图像。这里是我的图像屏幕截图

下面是我的代码

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.insertSubview(backgroundImage, at:0)

请帮帮我谢谢

fwzugrvs

fwzugrvs1#

您的UIImageView的高度与屏幕大小相同,如果您的视图大于屏幕大小,则白色视图将保留在底部。

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)

使用滚动视图更新backgroundImage的帧,如下所示:

let backgroundImage = UIImageView(frame: self.my_view_2.bounds)
backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth] // In case if your my_view_2 frames increases
self.my_view_2.insertSubview(backgroundImage, at: 0)
eivgtgni

eivgtgni2#

把这段代码加到你的viewDidLoad()中.可能会帮助你解决.

if #available(iOS 11.0, *) {
   scrollView.contentInsetAdjustmentBehavior = .never
} else {
   automaticallyAdjustsScrollViewInsets = false
}
7lrncoxx

7lrncoxx3#

试试这个

let backgroundImage = UIImageView(frame: my_view_2.bounds)
backgroundImage.clipsToBounds = true
backgroundImage.image = UIImage(named: "background_image")
backgroundImage.contentMode = .scaleToFill
self.my_view_2.addSubview(backgroundImage)
yb3bgrhw

yb3bgrhw4#

希望能有所帮助

self.my_view_2.layer.contents = #imageLiteral(resourceName: "background_image");

相关问题