gpt4 book ai didi

ios - 如何让图像覆盖整个UIView?

转载 作者:行者123 更新时间:2023-11-30 11:52:28 25 4
gpt4 key购买 nike

我有下面的代码,将图像放置在 View 上。问题是从 iPhone 传输到 iPad 时,创建固定的高度和宽度不起作用。我希望代码能够传输整个 Canvas ,而不是固定数量。

@IBOutlet weak var jj: draw!

override func viewDidLoad() {
super.viewDidLoad()

let image = UIImage(named: "se")
let imageView = UIImageView(image: image!)
imageView.frame = CGRect(x: 0, y: 0, width: 200, height: 300)
jj.addSubview(imageView)
}

最佳答案

您可以轻松地使用自动布局来完成此任务。

      let backgroundImage : UIImageView = {
let image = UIImageView(image: #imageLiteral(resourceName: "name-of-image"))
image.translatesAutoresizingMaskIntoConstraints = false
image.contentMode = .scaleToFill
return image
}()

override func viewDidLoad() {
super.viewDidLoad()
setUpView()
}

func setUpView(){
view.addSubview(backgroundImage)
NSLayoutConstraint.activate([
backgroundImage.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
backgroundImage.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
backgroundImage.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
backgroundImage.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
])
}

关于ios - 如何让图像覆盖整个UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48199106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com