BiHMaster
BiHMaster

Reputation: 63

View Controller (UIViewController) Storyboard with multiple scenes using same background image

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.

Upvotes: 1

Views: 1392

Answers (2)

BiHMaster
BiHMaster

Reputation: 63

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.

Upvotes: 1

xmhafiz
xmhafiz

Reputation: 3538

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

Upvotes: 0

Related Questions