gpt4 book ai didi

ios - 如何使用 swift 将相同的背景图像添加到我的 ios 应用程序中的所有 View ?

转载 作者:行者123 更新时间:2023-11-30 11:52:16 25 4
gpt4 key购买 nike

我希望能够向我的 ios 应用程序添加适用于所有 View 的背景图像,而不必将此图像添加到每个 View 。是否可以向 UIWindow 添加图像或其他内容?

现在,当我进行转换时,新 View 的背景也会“转换”到我之前的 View 上,即使其背景相同。我希望能够只看到内容转换,而不是背景,这是相同的。

最佳答案

您应该能够通过以下方式实现这一目标:

  1. 子类化 UIWindow 并重写 drawRect 来绘制图像;有关子类化 UIWindow 的详细信息 here
  2. 然后使用 UIAppearance 控制应用中其余 View 的背景颜色(您应该将其设置为 clearColor);有关此的详细信息 here ,尽管我认为您需要对 Controller 使用的 View 进行子类化,以便处理已接受答案中的注释

基本实现可能如下所示:

class WallpaperWindow: UIWindow {

var wallpaper: UIImage? = UIImage(named: "wallpaper") {
didSet {
// refresh if the image changed
setNeedsDisplay()
}
}

init() {
super.init(frame: UIScreen.mainScreen().bounds)
//clear the background color of all table views, so we can see the background
UITableView.appearance().backgroundColor = UIColor.clearColor()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override func drawRect(rect: CGRect) {
// draw the wallper if set, otherwise default behaviour
if let wallpaper = wallpaper {
wallpaper.drawInRect(self.bounds);
} else {
super.drawRect(rect)
}
}
}

在 AppDelegate 中:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow? = WallpaperWindow()

关于ios - 如何使用 swift 将相同的背景图像添加到我的 ios 应用程序中的所有 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48218509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com