ios - 图像未根据方向变化调整大小

标签 ios swift uiimageview

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

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

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

最佳答案

而不是使用框架

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) 
])

关于ios - 图像未根据方向变化调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910629/

相关文章:

ios - 如何以编程方式在 uiview 上调整图像

ios - 方向改变时的图像拉伸(stretch)

ios - UIView 外观 semanticContentAttribute 用于特定 View IOS

ios - 意外发现 nil URL SWIFT

objective-c - iOS - iPhone 5 无法滚动到 TableView 的底部

swift - 隐藏搜索栏也隐藏导航栏标题

swift - 如何在 macOS 中访问受限文件 (/User/user/Library/Messages/xxx)?

ios - 重用 ScrollView

ios - 附属 Action 与选择转场

ios - Swift MKPointAnnotation 自定义图像