从图像视图设置视图背景

问题描述 投票:0回答:1

我正在尝试将UIImageView中放置的任何图像与视图控制器视图的背景图像进行匹配。因此,在下面的示例中,当用户调用func call时,图像视图choosenBack中的任何图像都会显示为视图控制器的背景。如果图像视图中未放置任何图像,则视图背景图像应仅为nil

choosenBack = UIImageView()

func call(){
    self.view.backgroundColor == UIColor(patternImage: UIImage(choosenBack)!)
}
swift uiview uiviewcontroller uiimageview uiimage
1个回答
0
投票

使用backgroundColor属性仅在您确实希望重复图像以填充背景时才起作用。在这种情况下,您可以简单地执行

func call() {
    if let image = choosenBack.image {
        self.view.backgroundColor = UIColor(patternImage: image)
    } else {
        self.view.backgroundColor = .white // Or w/e your default background is
    }
}

如果要重复[[不背景图像,则需要使用专用的背景图像视图。

let background = UIImageView() override func viewDidLoad() { super.viewDidLoad() background.contentMode = .scaleAspectFit // Or w/e your desired content mode is view.insertSubview(background, at: 0) background.translatesAutoresizingMaskIntoConstraints = false background.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true background.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true background.topAnchor.constraint(equalTo: view.topAnchor).isActive = true background.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true ... } func call() { self.background.image = choosenBack.image }
© www.soinside.com 2019 - 2024. All rights reserved.