簡體   English   中英

自動調整背景圖像以適應所有設備和方向 Swift

[英]Auto Resize Background Image To Fit All Devices And Orientations 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

在縱向和橫向模式下,沒有一個圖像適合您。 您可能必須實施自適應設計

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM