首页 > 解决方案 > 自动调整背景图像大小以适应所有设备和方向 Swift

问题描述

我正在使用 Swift 5 和 Xcode 11 为我的 iOS 选项卡式应用程序制作一些丰富多彩的主题。我有多个带有背景图像的主题。这些背景图像具有适合纵向的完美纵横比,因此,它们的工作方式与我希望它们在纵向时完全一致: 在此处输入图像描述

但是当我把我的设备放到风景中时,事情变得很不稳定: 在此处输入图像描述

图像重复。有什么办法可以解决这个问题吗?我在 StackOverflow 上阅读了一些其他类似的问题,但都没有结果。

这是我的视图控制器的“主题”功能的一部分,每次视图加载或视图出现时都会调用它(这些只是我的两个主题):

func theme(){
        if UserDefaults.standard.integer(forKey: "like") == 1{
            self.tabBarController?.tabBar.barTintColor = UIColor(red:0.84, green:0.84, blue:0.84, alpha:1.0)
            self.tabBarController?.tabBar.tintColor = UIColor(red:0.21, green:0.56, blue:0.96, alpha:1.0)
            view1.backgroundColor = UIColor(red:0.78, green:0.78, blue:0.78, alpha:1.0)
            ansView.backgroundColor = UIColor.white
            historyButton.backgroundColor = UIColor.black
            for button in operatorButtons {
                button.backgroundColor = UIColor.black
            }
            for button in numberButtons {
                button.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
            }
            equalButton.backgroundColor = UIColor(red:1.00, green:0.49, blue:0.18, alpha:1.0)
            self.view.backgroundColor = UIColor(red:0.78, green:0.78, blue:0.78, alpha:1.0)
            view1.contentMode = UIView.ContentMode.scaleAspectFill
        }else if UserDefaults.standard.integer(forKey: "like") == 2{
            self.tabBarController?.tabBar.barTintColor = UIColor.black
            self.tabBarController?.tabBar.tintColor = UIColor(red:0.98, green:0.64, blue:0.02, alpha:1.0)
            view1.backgroundColor = UIColor(patternImage: UIImage(named: "Black-6")!)
            ansView.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
            historyButton.backgroundColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.9)
            for button in operatorButtons {
                button.backgroundColor = UIColor(red:0.00, green:0.00, blue:0.00, alpha:0.9)
            }
            for button in numberButtons {
                button.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
            }
            equalButton.backgroundColor = UIColor(red:0.53, green:0.52, blue:0.53, alpha:0.5)
            self.view.backgroundColor = UIColor.black
            view1.contentMode = UIView.ContentMode.scaleAspectFill
        }
//more themes
    }

顺便说一句,我试过:

view1.contentMode = UIView.ContentMode.scaleAspectFit

和:

view1.contentMode = UIView.ContentMode.scaleToFill

标签: iosswift

解决方案


在纵向和横向模式下,没有任何一张图片适合您。您可能必须实施自适应设计


推荐阅读