1

I am developing a game where the first View Controller will contain full functionality for the rest of the 30 View Controllers, where the rest of these controllers will be sublass of the first view controller. My plan is to use single storyboard for all 30 view controllers or scenes in my app which will all use the same background image. To give you an idea as to what I'm talking about, I only show 2 scenes in this Drawing.Storyboard image but plan is to have 28 more scenes in this same storyboard. Drawing.Storyboard

If all 30 scenes in this storyboard will have the same UIView background image, how to handle that. Will I have to add same background image for each view in scene or just add background image to the first scene view and use container view for the rest? Note I have never use container views in the past.

BiHMaster
  • 63
  • 1
  • 12
  • Use Extension for create background image and just call it from all controller you don't need to add it into storyboard – Himanshu Moradiya Oct 19 '16 at 04:28
  • Try to set image in Appdelegate's window as below link : http://stackoverflow.com/questions/4915474/set-background-image-for-entire-iphone-ipad-app?answertab=votes#tab-top – KKRocks Oct 19 '16 at 05:20
  • Himanshu, if I go your way, would you be able to post answer or provide an example? Thanks. – BiHMaster Oct 19 '16 at 22:37

2 Answers2

1

After further research and suggestion by "h44f33z", the following will work without using UIView image in your storyboard.

ViewController A

class ViewControllerA: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Load background image for all views
    let bgImageView = UIImageView(frame: UIScreen.main.bounds)
    bgImageView.image = UIImage(named: "bg_image")
    self.view.addSubview(bgImageView)
  }
}

View Controller B

class ViewControllerB: ViewControllerA {
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

  }
}

There is nothing you have to do in ViewController B because it is a subclass of ViewController A. With this setup you can go with as many as possible views as long as view is subclass of the first view controller.

BiHMaster
  • 63
  • 1
  • 12
0

One way to do that, you can just create a parent / base class of UIViewController and add UIImageView to self.view in viewDidLoad()

So, for the all 30 ViewControllers, should extend to that base viewController. It will look similar to this

class StartViewController: BaseViewController

Your base class viewDidLoad will be something like

override func viewDidLoad() {
    super.viewDidLoad()

    let bgImageView = UIImageView(frame: UIScreen.mainScreen().bounds)
    bgImageView.image = UIImage(named: "bg-image")
    self.view.addSubview(bgImageView)

}

You can event add more functions or handling in the base class and will be easily used by all 30 child viewControllers

xmhafiz
  • 3,482
  • 1
  • 18
  • 26
  • If I add this code and remove bg_image view that had my background image from my storyboard for second view controller, bg_image never loads in my second view controller which is subclass of first view controller. Any other or further suggestions! Thanks. – BiHMaster Oct 19 '16 at 12:43
  • what do you mean first view controller? the base controller? – xmhafiz Oct 19 '16 at 15:08
  • Basically I have ViewControllerA and ViewControllerB. Everything I need is in ViewControllerA or base and ViewControllerB is subclass of ViewControllerA. If I add this code in ViewControllerB, image never loads. Basically ViewControllerB has it's own scene with UIView. When I remove that UIView and use this code to load bg_image, it never loads. – BiHMaster Oct 19 '16 at 15:16