Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

Commit

Permalink
Colors! Special thanks to @dexcell for custom colors idea.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilter Cengiz committed Sep 21, 2013
1 parent 26c1792 commit 7c3b05c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 15 deletions.
12 changes: 12 additions & 0 deletions ICViewPager/Controller/HostViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions ICViewPager/ICViewPager/ViewPagerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) {
ViewPagerOptionCenterCurrentTab
};

typedef NS_ENUM(NSUInteger, ViewPagerComponent) {
ViewPagerIndicator,
ViewPagerTabsView,
ViewPagerContent
};

@protocol ViewPagerDataSource;
@protocol ViewPagerDelegate;

Expand Down Expand Up @@ -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 <NSObject>

// Asks dataSource how many tabs will be
Expand All @@ -67,6 +80,7 @@ typedef NS_ENUM(NSUInteger, ViewPagerOption) {

@end

#pragma mark delegate
@protocol ViewPagerDelegate <NSObject>

@optional
Expand All @@ -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
31 changes: 22 additions & 9 deletions ICViewPager/ICViewPager/ViewPagerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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];
}
}
Expand Down Expand Up @@ -243,6 +241,7 @@ - (void)setActiveTabIndex:(NSUInteger)activeTabIndex {
#pragma mark -
- (void)defaultSettings {

// Default settings
_tabHeight = kDefaultTabHeight;
_tabOffset = kDefaultTabOffset;
_tabWidth = kDefaultTabWidth;
Expand All @@ -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];
Expand Down Expand Up @@ -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];
Expand All @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions ICViewPager/iPad.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4504" systemVersion="12E55" targetRuntime="iOS.CocoaTouch.iPad" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="vsM-tZ-2I8">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4510" systemVersion="12F37" targetRuntime="iOS.CocoaTouch.iPad" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="vsM-tZ-2I8">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3734.1"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3742"/>
</dependencies>
<scenes>
<!--Host View Controller-->
Expand Down Expand Up @@ -44,7 +44,7 @@
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</view>
<connections>
<outlet property="label" destination="I2X-5R-hg1" id="j0v-JU-juP"/>
Expand Down
6 changes: 3 additions & 3 deletions ICViewPager/iPhone.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="4504" systemVersion="12E55" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="KZy-Tn-xJf">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="2.0" toolsVersion="4510" systemVersion="12F37" targetRuntime="iOS.CocoaTouch" variant="6xAndEarlier" propertyAccessControl="none" useAutolayout="YES" initialViewController="KZy-Tn-xJf">
<dependencies>
<deployment defaultVersion="1552" identifier="iOS"/>
<development version="4600" identifier="xcode"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3734.1"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3742"/>
</dependencies>
<scenes>
<!--Host View Controller-->
Expand Down Expand Up @@ -39,7 +39,7 @@
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="dAg-1K-keU" secondAttribute="trailing" constant="20" symbolic="YES" type="default" id="NKg-px-ryJ"/>
<constraint firstItem="dAg-1K-keU" firstAttribute="top" secondItem="Rhb-jG-fQ8" secondAttribute="top" constant="20" symbolic="YES" type="default" id="aTN-V5-W4B"/>
Expand Down

0 comments on commit 7c3b05c

Please sign in to comment.