自动调整背景图像大小以适应所有设备和方向

时间:2020-01-21 17:29:34

标签: ios swift

我正在使用Swift 5和Xcode 11为我的iOS选项卡应用程序制作一些丰富多彩的主题。我有多个带有背景图片的主题。这些背景图像具有完美的纵向显示纵横比,因此,它们的工作方式完全符合我想要的纵向显示方式: enter image description here

但是,当我将设备放到风景中时,事情变得不妙了: enter image description here

图像重复。有什么办法可以解决这个问题?我已经在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

1 个答案:

答案 0 :(得分:1)

在纵向和横向模式下都没有适合您的图像。您可能必须实现adaptive design

相关问题