ios - 自动调整背景图像大小以适应所有设备和方向 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
解决方案
在纵向和横向模式下,没有任何一张图片适合您。您可能必须实施自适应设计。
推荐阅读
- c# - 尝试使用 update 命令更新 SQL 数据库
- xml - 使用 xmlstarlet 将 XML 转换为 CSV
- sql-server - Spring Batch无效的对象名称'BATCH_JOB_INSTANCE'
- maven - Nexus 存储库匿名下载:错误还是功能?
- xml - 如何使用 XSLT 替换 XML 节点名称中的字符 - 更改根元素
- reactjs - 反应本机日历标记日期
- java - 在 rest API 中应用 @OrderBy 注释而不是排序
- c++ - 使用输入 C++ 从 .txt 文件中打印 N 行
- list - HASKELL:将列表列表的第一个 x 元素添加到另一个列表中
- java - Java 中的正则表达式电子邮件验证问题