设置视图背景图像等于图像视图背景图像

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

我想做的就是匹配uiimageview中放置的任何图像。到视图控制器视图的背景图像。因此,当用户调用func call时,图像视图中的任何图像都会在viewcontroller视图的背景颜色上显示selectedn。如果图像视图上没有图像,则视图背景图像应为null

choosenBack = UIImageView()

func call(){
    self.view.backgroundColor == UIColor(patternImage: UIImage(choosenBack)!)
}
swift uiview uiviewcontroller uiimage background-color
1个回答
0
投票
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.