Change all UIViewControllers Backgrounds










1














I want to change all my project's UIViewControllers, so I have this:



let bg = BackgroundImage().backgroundImage
bg.alpha = 0.2
bg.frame = self.view.frame

let wBg = UIView()
wBg.frame = self.view.frame
wBg.backgroundColor = UIColor(white: 1, alpha: 0.5)

self.view.addSubview(wBg)
self.view.sendSubview(toBack: wBg)

self.view.addSubview(bg)
self.view.sendSubview(toBack: bg)


Where BackgroundImage().backgroundImage returns a UIImageView.



But how can I implement this on all my UIViewControllers without having to copy/paste the same in all my VC.










share|improve this question




























    1














    I want to change all my project's UIViewControllers, so I have this:



    let bg = BackgroundImage().backgroundImage
    bg.alpha = 0.2
    bg.frame = self.view.frame

    let wBg = UIView()
    wBg.frame = self.view.frame
    wBg.backgroundColor = UIColor(white: 1, alpha: 0.5)

    self.view.addSubview(wBg)
    self.view.sendSubview(toBack: wBg)

    self.view.addSubview(bg)
    self.view.sendSubview(toBack: bg)


    Where BackgroundImage().backgroundImage returns a UIImageView.



    But how can I implement this on all my UIViewControllers without having to copy/paste the same in all my VC.










    share|improve this question


























      1












      1








      1







      I want to change all my project's UIViewControllers, so I have this:



      let bg = BackgroundImage().backgroundImage
      bg.alpha = 0.2
      bg.frame = self.view.frame

      let wBg = UIView()
      wBg.frame = self.view.frame
      wBg.backgroundColor = UIColor(white: 1, alpha: 0.5)

      self.view.addSubview(wBg)
      self.view.sendSubview(toBack: wBg)

      self.view.addSubview(bg)
      self.view.sendSubview(toBack: bg)


      Where BackgroundImage().backgroundImage returns a UIImageView.



      But how can I implement this on all my UIViewControllers without having to copy/paste the same in all my VC.










      share|improve this question















      I want to change all my project's UIViewControllers, so I have this:



      let bg = BackgroundImage().backgroundImage
      bg.alpha = 0.2
      bg.frame = self.view.frame

      let wBg = UIView()
      wBg.frame = self.view.frame
      wBg.backgroundColor = UIColor(white: 1, alpha: 0.5)

      self.view.addSubview(wBg)
      self.view.sendSubview(toBack: wBg)

      self.view.addSubview(bg)
      self.view.sendSubview(toBack: bg)


      Where BackgroundImage().backgroundImage returns a UIImageView.



      But how can I implement this on all my UIViewControllers without having to copy/paste the same in all my VC.







      ios swift






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 0:16









      rmaddy

      239k27311376




      239k27311376










      asked Nov 13 '18 at 0:13









      Cesar RodriguezCesar Rodriguez

      689




      689






















          2 Answers
          2






          active

          oldest

          votes


















          1














          One solution is to create a function in a UIViewController extension. This function should setup the view controller's background as needed.



          Then in the viewDidLoad function of each of your view controllers you can call that extension function.






          share|improve this answer
















          • 1




            GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
            – Cesar Rodriguez
            Nov 13 '18 at 0:41



















          1














          1. Create BaseViewController.swift



          class BaseViewController: UIViewController 
          override func viewDidLoad()
          setBackgroundImage()


          setBackgroundImage()





          2. Other ViewController will inherit the BaseViewController



          class TimelinesViewController: BaseViewController 







          share|improve this answer
















          • 1




            The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
            – rmaddy
            Nov 13 '18 at 3:06










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271959%2fchange-all-uiviewcontrollers-backgrounds%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          One solution is to create a function in a UIViewController extension. This function should setup the view controller's background as needed.



          Then in the viewDidLoad function of each of your view controllers you can call that extension function.






          share|improve this answer
















          • 1




            GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
            – Cesar Rodriguez
            Nov 13 '18 at 0:41
















          1














          One solution is to create a function in a UIViewController extension. This function should setup the view controller's background as needed.



          Then in the viewDidLoad function of each of your view controllers you can call that extension function.






          share|improve this answer
















          • 1




            GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
            – Cesar Rodriguez
            Nov 13 '18 at 0:41














          1












          1








          1






          One solution is to create a function in a UIViewController extension. This function should setup the view controller's background as needed.



          Then in the viewDidLoad function of each of your view controllers you can call that extension function.






          share|improve this answer












          One solution is to create a function in a UIViewController extension. This function should setup the view controller's background as needed.



          Then in the viewDidLoad function of each of your view controllers you can call that extension function.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 0:29









          rmaddyrmaddy

          239k27311376




          239k27311376







          • 1




            GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
            – Cesar Rodriguez
            Nov 13 '18 at 0:41













          • 1




            GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
            – Cesar Rodriguez
            Nov 13 '18 at 0:41








          1




          1




          GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
          – Cesar Rodriguez
          Nov 13 '18 at 0:41





          GREAT! I created an BgImage+UIViewcontroller.swift extension then created the function setBackgroundImage() and done! Thanks @rmaddy!
          – Cesar Rodriguez
          Nov 13 '18 at 0:41














          1














          1. Create BaseViewController.swift



          class BaseViewController: UIViewController 
          override func viewDidLoad()
          setBackgroundImage()


          setBackgroundImage()





          2. Other ViewController will inherit the BaseViewController



          class TimelinesViewController: BaseViewController 







          share|improve this answer
















          • 1




            The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
            – rmaddy
            Nov 13 '18 at 3:06















          1














          1. Create BaseViewController.swift



          class BaseViewController: UIViewController 
          override func viewDidLoad()
          setBackgroundImage()


          setBackgroundImage()





          2. Other ViewController will inherit the BaseViewController



          class TimelinesViewController: BaseViewController 







          share|improve this answer
















          • 1




            The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
            – rmaddy
            Nov 13 '18 at 3:06













          1












          1








          1






          1. Create BaseViewController.swift



          class BaseViewController: UIViewController 
          override func viewDidLoad()
          setBackgroundImage()


          setBackgroundImage()





          2. Other ViewController will inherit the BaseViewController



          class TimelinesViewController: BaseViewController 







          share|improve this answer












          1. Create BaseViewController.swift



          class BaseViewController: UIViewController 
          override func viewDidLoad()
          setBackgroundImage()


          setBackgroundImage()





          2. Other ViewController will inherit the BaseViewController



          class TimelinesViewController: BaseViewController 








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 2:00









          GiangGiang

          1,3041214




          1,3041214







          • 1




            The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
            – rmaddy
            Nov 13 '18 at 3:06












          • 1




            The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
            – rmaddy
            Nov 13 '18 at 3:06







          1




          1




          The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
          – rmaddy
          Nov 13 '18 at 3:06




          The problem with this solution is that it doesn't work for table view controllers and collection view controllers and others.
          – rmaddy
          Nov 13 '18 at 3:06

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271959%2fchange-all-uiviewcontrollers-backgrounds%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto