Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPageViewController page control background color

i'm using UIPageViewController in my app and it's working fine. however, it's page control which has been added automatically has a black background which is hiding the current view controller's bottom material (See picture below). is it possible to call the UIPageViewController's page control and change it's color? i want the page control to be shown over the view controller (example, the Path app's walkthrough) like setting the color instead of black to clear.

enter image description here

like image 709
user1938695 Avatar asked Aug 23 '13 17:08

user1938695


3 Answers

You can use appearance to change the color of UIPageControl as otherwise it is not accessible. Try doing it in your AppDelegate's didFinishLaunchingWithOptions function as given below.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIPageControl *pageControl = [UIPageControl appearance];
    pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
    pageControl.backgroundColor = [UIColor blueColor];

    return YES;
}

To apply style only to a particular view controller, you can use appearanceWhenContainedIn instead as following:

UIPageControl *pageControl = [UIPageControl appearanceWhenContainedIn:[MyViewController class], nil];

Only UIPageControl objects contained in the MyViewController are going to get this style.

EDIT: The black background around UIPageControl at the bottom of your screen is due to the background color of your UIPageViewController not UIPageControl. You can change this color as following:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor]; //Set it to whatever you like
}
like image 107
Yas Tabasam Avatar answered Oct 19 '22 01:10

Yas Tabasam


Updated for Swift 3:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.main.bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clear
        }
    }
}

Swift 2 example for anyone that needs it. Put this inside your UIPageController subclass.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    for view in self.view.subviews {
        if view is UIScrollView {
            view.frame = UIScreen.mainScreen().bounds
        } else if view is UIPageControl {
            view.backgroundColor = UIColor.clearColor()
        }
    }
}
like image 28
jlichti Avatar answered Oct 19 '22 03:10

jlichti


Add the following code in the UIPageViewController.

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIPageControl appearance] setPageIndicatorTintColor: [UIColor grayColor]];
    [[UIPageControl appearance] setCurrentPageIndicatorTintColor: [UIColor whiteColor]];
    [[UIPageControl appearance] setBackgroundColor: [UIColor darkGrayColor]];
}
like image 10
Brandon Yang Avatar answered Oct 19 '22 03:10

Brandon Yang