简体   繁体   English

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

[英]Auto Resize Background Image To Fit All Devices And Orientations Swift

I am making some colorful themes for my iOS tabbed app using Swift 5 and Xcode 11. I have multiple themes with background images on them.我正在使用 Swift 5 和 Xcode 11 为我的 iOS 选项卡式应用程序制作一些彩色主题。我有多个带有背景图像的主题。 These background images have the perfect aspect ratio for portrait orientation and hence, they work exactly how I want them to in portrait orientation:这些背景图像具有完美的纵向纵横比,因此,它们的工作方式正是我希望它们在纵向时的效果: 在此处输入图片说明

But things go wonky when I put my device into the landscape:但是当我把我的设备放在风景中时,事情变得很奇怪: 在此处输入图片说明

The image duplicates.图像重复。 Is there any possible way I could solve this?有什么办法可以解决这个问题吗? I've read some other alike questions on StackOverflow, all to no results.我在 StackOverflow 上阅读了其他一些类似的问题,但都没有结果。

Here's part of my view controller's 'theme' function, which gets called every time the view loads or the view appears(these are just 2 of my themes):这是我的视图控制器的“主题”函数的一部分,每次视图加载或视图出现时都会调用它(这些只是我的两个主题):

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
    }

By the way, I have tried:顺便说一下,我试过:

view1.contentMode = UIView.ContentMode.scaleAspectFit

And:和:

view1.contentMode = UIView.ContentMode.scaleToFill

There is no single image that will work for you in both portrait and landscape mode.在纵向和横向模式下,没有一个图像适合您。 You might have to implement an adaptive design .您可能必须实施自适应设计

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM