【问题标题】:Image not resizing on orientation change图像未在方向更改时调整大小
【发布时间】:2018-08-19 01:13:47
【问题描述】:

在我的应用程序中,我将 imageView 设置为这样的背景图像。

let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "backgroundView.jpg")
backgroundImage.contentMode = .scaleAspectFill
view.insertSubview(backgroundImage, at: 0)

但是当我在横向模式下旋转模拟器时,图像会被截断。 附言我还注意到,如果我在风景中运行我的应用程序,那么即使我旋转到纵向并返回它也非常适合。

【问题讨论】:

    标签: ios swift uiimageview


    【解决方案1】:

    而不是使用框架

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

    你需要使用自动布局

    let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
    backgroundImage.translatesAutoresizingMaskIntoConstraints = false
    backgroundImage.image = UIImage(named: "backgroundView.jpg")
    backgroundImage.contentMode = .scaleAspectFill
    view.insertSubview(backgroundImage, at: 0)
    NSLayoutConstraint.activate([
        backgroundImage.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant:0),
        backgroundImage.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant:0),
        backgroundImage.topAnchor.constraint(equalTo: view.topAnchor,constant:0),
        backgroundImage.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant:0) 
    ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      相关资源
      最近更新 更多