Dynamically change the background color of the dots of UIPageControl.appearance





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I have a UIPagevViewController which is used to show 4 ViewControllers. I want the dots background color to change based on the view's background color I am currently into. While I have managed to get the same background color for both the dots and the first page I fail to do so for every other one. In other words I want to dynamically change the dots background based on the view that is shown



           import UIKit

class PageViewController: UIPageViewController,UIPageViewControllerDelegate, UIPageViewControllerDataSource {

//my 4 viewControllers used in the UIPageViewController
fileprivate lazy var pages : [UIViewController] = {
return [
self.getViewController(withIdentifier: "firstViewControllerID"),
self.getViewController(withIdentifier: "secondViewControllerID"),
self.getViewController(withIdentifier: "thirdViewControllerID"),
self.getViewController(withIdentifier: "differentViewController")

]
}()

//function that accesses those view controllers used above from the storyboard
fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
}
/* needed function for the UIPageViewControllerDelegate, UIPageViewControllerDataSource protocols
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
//Some Code. Unnecessary for the question
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
//Some Code. Unnecessary for the question
}
*/
//function where the dots colours are set
private func setupPageControl() {
let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
appearance.pageIndicatorTintColor = UIColor.gray
appearance.currentPageIndicatorTintColor = UIColor.red
appearance.backgroundColor = pages[appearance.currentPage].view.backgroundColor
}

func presentationCount(for pageViewController: UIPageViewController) -> Int {
setupPageControl()
return pages.count
}


func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}




override func viewDidLoad() {
super.viewDidLoad()


self.dataSource = self
delegate = self
if let firstVC = pages.first {
setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
setupPageControl()

}
}



}


enter image description here



enter image description here



First image->first page. Second image->second page. Look at the color of the dots background










share|improve this question





























    1















    I have a UIPagevViewController which is used to show 4 ViewControllers. I want the dots background color to change based on the view's background color I am currently into. While I have managed to get the same background color for both the dots and the first page I fail to do so for every other one. In other words I want to dynamically change the dots background based on the view that is shown



               import UIKit

    class PageViewController: UIPageViewController,UIPageViewControllerDelegate, UIPageViewControllerDataSource {

    //my 4 viewControllers used in the UIPageViewController
    fileprivate lazy var pages : [UIViewController] = {
    return [
    self.getViewController(withIdentifier: "firstViewControllerID"),
    self.getViewController(withIdentifier: "secondViewControllerID"),
    self.getViewController(withIdentifier: "thirdViewControllerID"),
    self.getViewController(withIdentifier: "differentViewController")

    ]
    }()

    //function that accesses those view controllers used above from the storyboard
    fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
    {
    return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
    }
    /* needed function for the UIPageViewControllerDelegate, UIPageViewControllerDataSource protocols
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
    //Some Code. Unnecessary for the question
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
    {
    //Some Code. Unnecessary for the question
    }
    */
    //function where the dots colours are set
    private func setupPageControl() {
    let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
    appearance.pageIndicatorTintColor = UIColor.gray
    appearance.currentPageIndicatorTintColor = UIColor.red
    appearance.backgroundColor = pages[appearance.currentPage].view.backgroundColor
    }

    func presentationCount(for pageViewController: UIPageViewController) -> Int {
    setupPageControl()
    return pages.count
    }


    func presentationIndex(for pageViewController: UIPageViewController) -> Int {
    return 0
    }




    override func viewDidLoad() {
    super.viewDidLoad()


    self.dataSource = self
    delegate = self
    if let firstVC = pages.first {
    setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
    setupPageControl()

    }
    }



    }


    enter image description here



    enter image description here



    First image->first page. Second image->second page. Look at the color of the dots background










    share|improve this question

























      1












      1








      1








      I have a UIPagevViewController which is used to show 4 ViewControllers. I want the dots background color to change based on the view's background color I am currently into. While I have managed to get the same background color for both the dots and the first page I fail to do so for every other one. In other words I want to dynamically change the dots background based on the view that is shown



                 import UIKit

      class PageViewController: UIPageViewController,UIPageViewControllerDelegate, UIPageViewControllerDataSource {

      //my 4 viewControllers used in the UIPageViewController
      fileprivate lazy var pages : [UIViewController] = {
      return [
      self.getViewController(withIdentifier: "firstViewControllerID"),
      self.getViewController(withIdentifier: "secondViewControllerID"),
      self.getViewController(withIdentifier: "thirdViewControllerID"),
      self.getViewController(withIdentifier: "differentViewController")

      ]
      }()

      //function that accesses those view controllers used above from the storyboard
      fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
      {
      return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
      }
      /* needed function for the UIPageViewControllerDelegate, UIPageViewControllerDataSource protocols
      func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
      //Some Code. Unnecessary for the question
      }

      func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
      {
      //Some Code. Unnecessary for the question
      }
      */
      //function where the dots colours are set
      private func setupPageControl() {
      let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
      appearance.pageIndicatorTintColor = UIColor.gray
      appearance.currentPageIndicatorTintColor = UIColor.red
      appearance.backgroundColor = pages[appearance.currentPage].view.backgroundColor
      }

      func presentationCount(for pageViewController: UIPageViewController) -> Int {
      setupPageControl()
      return pages.count
      }


      func presentationIndex(for pageViewController: UIPageViewController) -> Int {
      return 0
      }




      override func viewDidLoad() {
      super.viewDidLoad()


      self.dataSource = self
      delegate = self
      if let firstVC = pages.first {
      setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
      setupPageControl()

      }
      }



      }


      enter image description here



      enter image description here



      First image->first page. Second image->second page. Look at the color of the dots background










      share|improve this question














      I have a UIPagevViewController which is used to show 4 ViewControllers. I want the dots background color to change based on the view's background color I am currently into. While I have managed to get the same background color for both the dots and the first page I fail to do so for every other one. In other words I want to dynamically change the dots background based on the view that is shown



                 import UIKit

      class PageViewController: UIPageViewController,UIPageViewControllerDelegate, UIPageViewControllerDataSource {

      //my 4 viewControllers used in the UIPageViewController
      fileprivate lazy var pages : [UIViewController] = {
      return [
      self.getViewController(withIdentifier: "firstViewControllerID"),
      self.getViewController(withIdentifier: "secondViewControllerID"),
      self.getViewController(withIdentifier: "thirdViewControllerID"),
      self.getViewController(withIdentifier: "differentViewController")

      ]
      }()

      //function that accesses those view controllers used above from the storyboard
      fileprivate func getViewController(withIdentifier identifier: String) -> UIViewController
      {
      return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: identifier)
      }
      /* needed function for the UIPageViewControllerDelegate, UIPageViewControllerDataSource protocols
      func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
      //Some Code. Unnecessary for the question
      }

      func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
      {
      //Some Code. Unnecessary for the question
      }
      */
      //function where the dots colours are set
      private func setupPageControl() {
      let appearance = UIPageControl.appearance(whenContainedInInstancesOf: [UIPageViewController.self])
      appearance.pageIndicatorTintColor = UIColor.gray
      appearance.currentPageIndicatorTintColor = UIColor.red
      appearance.backgroundColor = pages[appearance.currentPage].view.backgroundColor
      }

      func presentationCount(for pageViewController: UIPageViewController) -> Int {
      setupPageControl()
      return pages.count
      }


      func presentationIndex(for pageViewController: UIPageViewController) -> Int {
      return 0
      }




      override func viewDidLoad() {
      super.viewDidLoad()


      self.dataSource = self
      delegate = self
      if let firstVC = pages.first {
      setViewControllers([firstVC], direction: .forward, animated: true, completion: nil)
      setupPageControl()

      }
      }



      }


      enter image description here



      enter image description here



      First image->first page. Second image->second page. Look at the color of the dots background







      ios swift uipagecontrol






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 21:39









      KorpelKorpel

      1,6911626




      1,6911626
























          1 Answer
          1






          active

          oldest

          votes


















          3














          Unfortunately you have no direct access to the page control built into a UIPageViewController. Therefore you have to use the appearance proxy in order to change its appearance (as you are already doing). But the appearance proxy only affects future instances of a thing; once the page control is already present, therefore, setting the appearance proxy will have no affect (as you have discovered).



          Therefore the simplest solution is to give up on the page control built into the UIPageViewController and just use your own UIPageControl whose dots you can change directly. You will have to coordinate it with the page view controller but that is not difficult.



          Another possibility is to try to get direct access to the UIPageViewController's UIPageControl. That's fragile because there is no official access, but that technique has been discussed here, as for example in this answer.






          share|improve this answer


























          • So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

            – Korpel
            Nov 27 '18 at 6:17












          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%2f53489469%2fdynamically-change-the-background-color-of-the-dots-of-uipagecontrol-appearance%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          Unfortunately you have no direct access to the page control built into a UIPageViewController. Therefore you have to use the appearance proxy in order to change its appearance (as you are already doing). But the appearance proxy only affects future instances of a thing; once the page control is already present, therefore, setting the appearance proxy will have no affect (as you have discovered).



          Therefore the simplest solution is to give up on the page control built into the UIPageViewController and just use your own UIPageControl whose dots you can change directly. You will have to coordinate it with the page view controller but that is not difficult.



          Another possibility is to try to get direct access to the UIPageViewController's UIPageControl. That's fragile because there is no official access, but that technique has been discussed here, as for example in this answer.






          share|improve this answer


























          • So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

            – Korpel
            Nov 27 '18 at 6:17
















          3














          Unfortunately you have no direct access to the page control built into a UIPageViewController. Therefore you have to use the appearance proxy in order to change its appearance (as you are already doing). But the appearance proxy only affects future instances of a thing; once the page control is already present, therefore, setting the appearance proxy will have no affect (as you have discovered).



          Therefore the simplest solution is to give up on the page control built into the UIPageViewController and just use your own UIPageControl whose dots you can change directly. You will have to coordinate it with the page view controller but that is not difficult.



          Another possibility is to try to get direct access to the UIPageViewController's UIPageControl. That's fragile because there is no official access, but that technique has been discussed here, as for example in this answer.






          share|improve this answer


























          • So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

            – Korpel
            Nov 27 '18 at 6:17














          3












          3








          3







          Unfortunately you have no direct access to the page control built into a UIPageViewController. Therefore you have to use the appearance proxy in order to change its appearance (as you are already doing). But the appearance proxy only affects future instances of a thing; once the page control is already present, therefore, setting the appearance proxy will have no affect (as you have discovered).



          Therefore the simplest solution is to give up on the page control built into the UIPageViewController and just use your own UIPageControl whose dots you can change directly. You will have to coordinate it with the page view controller but that is not difficult.



          Another possibility is to try to get direct access to the UIPageViewController's UIPageControl. That's fragile because there is no official access, but that technique has been discussed here, as for example in this answer.






          share|improve this answer















          Unfortunately you have no direct access to the page control built into a UIPageViewController. Therefore you have to use the appearance proxy in order to change its appearance (as you are already doing). But the appearance proxy only affects future instances of a thing; once the page control is already present, therefore, setting the appearance proxy will have no affect (as you have discovered).



          Therefore the simplest solution is to give up on the page control built into the UIPageViewController and just use your own UIPageControl whose dots you can change directly. You will have to coordinate it with the page view controller but that is not difficult.



          Another possibility is to try to get direct access to the UIPageViewController's UIPageControl. That's fragile because there is no official access, but that technique has been discussed here, as for example in this answer.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 21:58

























          answered Nov 26 '18 at 21:52









          mattmatt

          335k47550747




          335k47550747













          • So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

            – Korpel
            Nov 27 '18 at 6:17



















          • So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

            – Korpel
            Nov 27 '18 at 6:17

















          So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

          – Korpel
          Nov 27 '18 at 6:17





          So the default behaviour is the one I am stuck dealing with and the only way to get what I want is to go with a custom UIPageController. Am I correct Matt?

          – Korpel
          Nov 27 '18 at 6:17




















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489469%2fdynamically-change-the-background-color-of-the-dots-of-uipagecontrol-appearance%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







          Popular posts from this blog

          To store a contact into the json file from server.js file using a class in NodeJS

          Redirect URL with Chrome Remote Debugging Android Devices

          Dieringhausen