我怎样才能添加相同的背景图像到我的IOS应用程序中的所有视图使用SWIFT?

我想能够添加一个背景图像到我的iOS应用程序适用于所有视图,而不必将此图像添加到每个视图。 是否有可能将图像添加到UIWindow什么的?

现在,当我进行过渡的时候,即使它的背景相同,新观点的背景也是“过渡”了我以前的观点。 我希望能够只有内容过渡,而不是背景,这是相同的。

你应该可以通过以下方式来实现这一点:

  1. 子类化UIWindow并重写drawRect来绘制图像; 有关在这里子类化UIWindow细节
  2. 然后通过使用UIAppearance来控制应用程序中其余视图的背景颜色(您应该将其设置为clearColor ); 有关这方面的细节,虽然我认为你需要inheritance你的控制器使用的视图,以应付接受的答案中的笔记

一个基本的实现可能是这样的:

 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()