From 7c3b05ce4445680449c7475391947f8858258577 Mon Sep 17 00:00:00 2001 From: Ilter Cengiz Date: Sat, 21 Sep 2013 17:29:26 +0300 Subject: [PATCH] Colors! Special thanks to @dexcell for custom colors idea. --- ICViewPager/Controller/HostViewController.m | 12 +++++++ ICViewPager/ICViewPager/ViewPagerController.h | 23 ++++++++++++++ ICViewPager/ICViewPager/ViewPagerController.m | 31 +++++++++++++------ ICViewPager/iPad.storyboard | 6 ++-- ICViewPager/iPhone.storyboard | 6 ++-- 5 files changed, 63 insertions(+), 15 deletions(-) diff --git a/ICViewPager/Controller/HostViewController.m b/ICViewPager/Controller/HostViewController.m index 7079798..a364a48 100644 --- a/ICViewPager/Controller/HostViewController.m +++ b/ICViewPager/Controller/HostViewController.m @@ -77,5 +77,17 @@ - (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerO return value; } +- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color { + + switch (component) { + case ViewPagerIndicator: + return [[UIColor redColor] colorWithAlphaComponent:0.64]; + break; + default: + break; + } + + return color; +} @end diff --git a/ICViewPager/ICViewPager/ViewPagerController.h b/ICViewPager/ICViewPager/ViewPagerController.h index effcc90..285db91 100644 --- a/ICViewPager/ICViewPager/ViewPagerController.h +++ b/ICViewPager/ICViewPager/ViewPagerController.h @@ -17,6 +17,12 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) { ViewPagerOptionCenterCurrentTab }; +typedef NS_ENUM(NSUInteger, ViewPagerComponent) { + ViewPagerIndicator, + ViewPagerTabsView, + ViewPagerContent +}; + @protocol ViewPagerDataSource; @protocol ViewPagerDelegate; @@ -45,12 +51,19 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) { // Defaults to NO @property CGFloat centerCurrentTab; +#pragma mark Colors +// Colors for several parts +@property UIColor *indicatorColor; +@property UIColor *tabsViewBackgroundColor; +@property UIColor *contentViewBackgroundColor; + #pragma mark Methods // Reload all tabs and contents - (void)reloadData; @end +#pragma mark dataSource @protocol ViewPagerDataSource // Asks dataSource how many tabs will be @@ -67,6 +80,7 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) { @end +#pragma mark delegate @protocol ViewPagerDelegate @optional @@ -75,5 +89,14 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) { // Every time - reloadData called, ViewPager will ask its delegate for option values // So you don't have to set options from ViewPager itself - (CGFloat)viewPager:(ViewPagerController *)viewPager valueForOption:(ViewPagerOption)option withDefault:(CGFloat)value; +/* + * Use this method to customize the look and feel. + * viewPager will ask its delegate for colors for its components. + * And if they are provided, it will use them, otherwise it will use default colors. + * Also not that, colors for tab and content views will change the tabView's and contentView's background + * (you should provide these views with a clearColor to see the colors), + * and indicator will change its own color. + */ +- (UIColor *)viewPager:(ViewPagerController *)viewPager colorForComponent:(ViewPagerComponent)component withDefault:(UIColor *)color; @end diff --git a/ICViewPager/ICViewPager/ViewPagerController.m b/ICViewPager/ICViewPager/ViewPagerController.m index 8e9ca01..fcf0af5 100644 --- a/ICViewPager/ICViewPager/ViewPagerController.m +++ b/ICViewPager/ICViewPager/ViewPagerController.m @@ -20,6 +20,10 @@ #define kPageViewTag 34 +#define kDefaultIndicatorColor [UIColor colorWithRed:178.0/255.0 green:203.0/255.0 blue:57.0/255.0 alpha:0.75] +#define kDefaultTabsViewBackgroundColor [UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:0.75] +#define kDefaultContentViewBackgroundColor [UIColor colorWithRed:248.0/255.0 green:248.0/255.0 blue:248.0/255.0 alpha:0.75] + // TabView for tabs, that provides un/selected state indicators @class TabView; @@ -66,17 +70,11 @@ - (void)drawRect:(CGRect)rect { bezierPath = [UIBezierPath bezierPath]; - // Set indicator color if provided any, otherwise use a default color - if (self.indicatorColor) { - [self.indicatorColor setStroke]; - } else { - [[UIColor colorWithRed:178.0/255.0 green:203.0/255.0 blue:57.0/255.0 alpha:0.75] setStroke]; - } - // Draw the indicator [bezierPath moveToPoint:CGPointMake(0.0, rect.size.height - 1.0)]; [bezierPath addLineToPoint:CGPointMake(rect.size.width, rect.size.height - 1.0)]; [bezierPath setLineWidth:5.0]; + [self.indicatorColor setStroke]; [bezierPath stroke]; } } @@ -243,6 +241,7 @@ - (void)setActiveTabIndex:(NSUInteger)activeTabIndex { #pragma mark - - (void)defaultSettings { + // Default settings _tabHeight = kDefaultTabHeight; _tabOffset = kDefaultTabOffset; _tabWidth = kDefaultTabWidth; @@ -253,6 +252,12 @@ - (void)defaultSettings { _centerCurrentTab = kDefaultCenterCurrentTab; + // Default colors + _indicatorColor = kDefaultIndicatorColor; + _tabsViewBackgroundColor = kDefaultTabsViewBackgroundColor; + _contentViewBackgroundColor = kDefaultContentViewBackgroundColor; + + // pageViewController _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil]; @@ -281,6 +286,13 @@ - (void)reloadData { _centerCurrentTab = [self.delegate viewPager:self valueForOption:ViewPagerOptionCenterCurrentTab withDefault:kDefaultCenterCurrentTab]; } + // Get colors if provided + if ([self.delegate respondsToSelector:@selector(viewPager:colorForComponent:withDefault:)]) { + _indicatorColor = [self.delegate viewPager:self colorForComponent:ViewPagerIndicator withDefault:kDefaultIndicatorColor]; + _tabsViewBackgroundColor = [self.delegate viewPager:self colorForComponent:ViewPagerTabsView withDefault:kDefaultTabsViewBackgroundColor]; + _contentViewBackgroundColor = [self.delegate viewPager:self colorForComponent:ViewPagerContent withDefault:kDefaultContentViewBackgroundColor]; + } + // Empty tabs and contents [_tabs removeAllObjects]; [_contents removeAllObjects]; @@ -304,7 +316,7 @@ - (void)reloadData { self.view.frame.size.width, self.tabHeight)]; _tabsView.autoresizingMask = UIViewAutoresizingFlexibleWidth; - _tabsView.backgroundColor = [UIColor clearColor]; + _tabsView.backgroundColor = self.tabsViewBackgroundColor; _tabsView.showsHorizontalScrollIndicator = NO; _tabsView.showsVerticalScrollIndicator = NO; @@ -339,7 +351,7 @@ - (void)reloadData { pageView = _pageViewController.view; pageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; - pageView.backgroundColor = [UIColor clearColor]; + pageView.backgroundColor = self.contentViewBackgroundColor; pageView.bounds = self.view.bounds; pageView.tag = kPageViewTag; @@ -389,6 +401,7 @@ - (TabView *)tabViewAtIndex:(NSUInteger)index { TabView *tabView = [[TabView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tabWidth, self.tabHeight)]; [tabView addSubview:tabViewContent]; [tabView setClipsToBounds:YES]; + [tabView setIndicatorColor:self.indicatorColor]; tabViewContent.center = tabView.center; diff --git a/ICViewPager/iPad.storyboard b/ICViewPager/iPad.storyboard index c06206c..c425f6c 100644 --- a/ICViewPager/iPad.storyboard +++ b/ICViewPager/iPad.storyboard @@ -1,8 +1,8 @@ - + - + @@ -44,7 +44,7 @@ - + diff --git a/ICViewPager/iPhone.storyboard b/ICViewPager/iPhone.storyboard index e10e2f4..e35b3ad 100644 --- a/ICViewPager/iPhone.storyboard +++ b/ICViewPager/iPhone.storyboard @@ -1,9 +1,9 @@ - + - + @@ -39,7 +39,7 @@ - +