Skip to content

Commit

Permalink
Add support for translucent tab bar.
Browse files Browse the repository at this point in the history
  • Loading branch information
robbdimitrov committed May 18, 2014
1 parent c45394c commit ea534a6
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 73 deletions.
45 changes: 22 additions & 23 deletions Example/Classes/App Delegate/RDVAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,32 @@ - (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
- (void)customizeInterface {
UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];

if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
[navigationBarAppearance setBackgroundImage:[UIImage imageNamed:@"navigationbar_background_tall"]
forBarMetrics:UIBarMetricsDefault];
} else {
[navigationBarAppearance setBackgroundImage:[UIImage imageNamed:@"navigationbar_background"]
forBarMetrics:UIBarMetricsDefault];

NSDictionary *textAttributes = nil;
UIImage *backgroundImage = nil;
NSDictionary *textAttributes = nil;

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];

if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
textAttributes = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:20],
NSForegroundColorAttributeName: [UIColor blackColor],
};
} else {
textAttributes = @{
NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
NSForegroundColorAttributeName: [UIColor blackColor],
};
} else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
textAttributes = @{
UITextAttributeFont: [UIFont boldSystemFontOfSize:20],
UITextAttributeTextColor: [UIColor blackColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
};
#endif
}
backgroundImage = [UIImage imageNamed:@"navigationbar_background"];

[navigationBarAppearance setTitleTextAttributes:textAttributes];
textAttributes = @{
UITextAttributeFont: [UIFont boldSystemFontOfSize:18],
UITextAttributeTextColor: [UIColor blackColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
};
#endif
}

[navigationBarAppearance setBackgroundImage:backgroundImage
forBarMetrics:UIBarMetricsDefault];
[navigationBarAppearance setTitleTextAttributes:textAttributes];
}

@end
14 changes: 13 additions & 1 deletion Example/Classes/Controllers/RDVFirstViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ - (void)viewDidLoad {
[super viewDidLoad];

[[self rdv_tabBarItem] setBadgeValue:@"3"];

if (self.rdv_tabBarController.tabBar.translucent) {
UIEdgeInsets insets = UIEdgeInsetsMake(0,
0,
CGRectGetHeight(self.rdv_tabBarController.tabBar.frame),
0);

self.tableView.contentInset = insets;
self.tableView.scrollIndicatorInsets = insets;
}
}

- (NSUInteger)supportedInterfaceOrientations {
Expand Down Expand Up @@ -80,13 +90,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];

[[self rdv_tabBarItem] setBadgeValue:[NSString stringWithFormat:@"%d", indexPath.row+1]];

[[self rdv_tabBarController] setTabBarHidden:!self.rdv_tabBarController.tabBarHidden animated:YES];
}

@end
17 changes: 16 additions & 1 deletion Example/Classes/Controllers/RDVSecondViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// THE SOFTWARE.

#import "RDVSecondViewController.h"
#import "RDVTabBarController.h"

@implementation RDVSecondViewController

Expand All @@ -36,6 +37,20 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

#pragma mark - View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];

if (self.rdv_tabBarController.tabBar.translucent) {
UIEdgeInsets insets = UIEdgeInsetsMake(0,
0,
CGRectGetHeight(self.rdv_tabBarController.tabBar.frame),
0);

self.tableView.contentInset = insets;
self.tableView.scrollIndicatorInsets = insets;
}
}

- (NSUInteger)supportedInterfaceOrientations {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
Expand Down Expand Up @@ -72,7 +87,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
17 changes: 16 additions & 1 deletion Example/Classes/Controllers/RDVThirdViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// THE SOFTWARE.

#import "RDVThirdViewController.h"
#import "RDVTabBarController.h"

@implementation RDVThirdViewController

Expand All @@ -36,6 +37,20 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

#pragma mark - View lifecycle

- (void)viewDidLoad {
[super viewDidLoad];

if (self.rdv_tabBarController.tabBar.translucent) {
UIEdgeInsets insets = UIEdgeInsetsMake(0,
0,
CGRectGetHeight(self.rdv_tabBarController.tabBar.frame),
0);

self.tableView.contentInset = insets;
self.tableView.scrollIndicatorInsets = insets;
}
}

- (NSUInteger)supportedInterfaceOrientations {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
Expand Down Expand Up @@ -72,7 +87,7 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
4 changes: 2 additions & 2 deletions RDVTabBarController.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = "RDVTabBarController"
s.version = "1.1.5"
s.version = "1.1.6"
s.summary = "Highly customizable tabBar and tabBarController for iOS"
s.description = "RDVTabBarController is iPad and iPhone compatible. Supports landscape and portrait orientations and can be used inside UINavigationController."
s.homepage = "https://github.com/robbdimitrov/RDVTabBarController"
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { "Robert Dimitrov" => "[email protected]" }
s.platform = :ios, '5.0'
s.source = { :git => "https://github.com/robbdimitrov/RDVTabBarController.git", :tag => "v1.1.5" }
s.source = { :git => "https://github.com/robbdimitrov/RDVTabBarController.git", :tag => "v1.1.6" }
s.source_files = 'RDVTabBarController', 'RDVTabBarController/**/*.{h,m}'
s.framework = 'UIKit', 'CoreGraphics', 'Foundation'
s.requires_arc = true
Expand Down
5 changes: 5 additions & 0 deletions RDVTabBarController/RDVTabBar.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@
*/
- (CGFloat)minimumContentHeight;

/*
* Enable or disable tabBar translucency. Default is NO.
*/
@property (nonatomic, getter=isTranslucent) BOOL translucent;

@end
16 changes: 15 additions & 1 deletion RDVTabBarController/RDVTabBar.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ - (id)init {

- (void)commonInitialization {
_backgroundView = [[UIView alloc] init];
[_backgroundView setBackgroundColor:[UIColor lightGrayColor]];
[self addSubview:_backgroundView];

[self setTranslucent:NO];
}

- (void)layoutSubviews {
Expand Down Expand Up @@ -155,4 +156,17 @@ - (void)setSelectedItem:(RDVTabBarItem *)selectedItem {
[_selectedItem setSelected:YES];
}

#pragma mark - Translucency

- (void)setTranslucent:(BOOL)translucent {
_translucent = translucent;

CGFloat alpha = (translucent ? 0.9 : 1.0);

[_backgroundView setBackgroundColor:[UIColor colorWithRed:245/255.0
green:245/255.0
blue:245/255.0
alpha:alpha]];
}

@end
51 changes: 15 additions & 36 deletions RDVTabBarController/RDVTabBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,9 @@ - (void)viewDidLoad {
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

CGSize viewSize = self.view.frame.size;
CGFloat tabBarHeight = CGRectGetHeight([[self tabBar] frame]);
CGFloat tabBarStartingY = viewSize.height;
CGFloat contentViewHeight = viewSize.height;
if (!tabBarHeight) {
tabBarHeight = 49;
}

if (![self isTabBarHidden]) {
tabBarStartingY = viewSize.height - tabBarHeight;
contentViewHeight = viewSize.height - ([[self tabBar] minimumContentHeight] ?: tabBarHeight);
}

[[self tabBar] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
[[self contentView] setFrame:CGRectMake(0, 0, viewSize.width, contentViewHeight)];

[self setSelectedIndex:[self selectedIndex]];

[self setTabBarHidden:NO animated:NO];
}

- (NSUInteger)supportedInterfaceOrientations {
Expand Down Expand Up @@ -179,43 +165,36 @@ - (UIView *)contentView {
}

- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
if (_tabBarHidden == hidden) {
return;
}

_tabBarHidden = hidden;

__weak RDVTabBarController *weakSelf = self;

void (^block)() = ^{
CGSize viewSize = weakSelf.view.frame.size;
CGRect contentViewFrame = [[weakSelf contentView] frame];
CGRect tabBarFrame = [[weakSelf tabBar] frame];
CGFloat tabBarStartingY = viewSize.height;
CGFloat contentViewHeight = viewSize.height;
CGFloat tabBarHeight = CGRectGetHeight([[weakSelf tabBar] frame]);

if (!tabBarHeight) {
tabBarHeight = 49;
}

if (![weakSelf parentViewController]) {
if (UIInterfaceOrientationIsLandscape([weakSelf interfaceOrientation])) {
viewSize = CGSizeMake(viewSize.height, viewSize.width);
}
}

CGFloat tabBarStartingY = viewSize.height;
CGFloat contentViewHeight = viewSize.height;

if (!hidden) {
tabBarStartingY = viewSize.height - CGRectGetHeight(tabBarFrame);
contentViewHeight = viewSize.height - [[weakSelf tabBar] minimumContentHeight];
tabBarStartingY = viewSize.height - tabBarHeight;
if (![[weakSelf tabBar] isTranslucent]) {
contentViewHeight -= ([[weakSelf tabBar] minimumContentHeight] ?: tabBarHeight);
}
[[weakSelf tabBar] setHidden:NO];
}

[[weakSelf tabBar] setFrame:CGRectMake(CGRectGetMinX(tabBarFrame),
tabBarStartingY,
CGRectGetWidth(tabBarFrame),
CGRectGetHeight(tabBarFrame))];

[[weakSelf contentView] setFrame:CGRectMake(CGRectGetMinX(contentViewFrame),
CGRectGetMinY(contentViewFrame),
CGRectGetWidth(contentViewFrame),
contentViewHeight)];
[[weakSelf tabBar] setFrame:CGRectMake(0, tabBarStartingY, viewSize.width, tabBarHeight)];
[[weakSelf contentView] setFrame:CGRectMake(0, 0, viewSize.width, contentViewHeight)];
};

void (^completion)(BOOL) = ^(BOOL finished){
Expand Down
10 changes: 6 additions & 4 deletions RDVTabBarController/RDVTabBarItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ - (id)init {
- (void)commonInitialization {
// Setup defaults

[self setBackgroundColor:[UIColor clearColor]];

_title = @"";
_titlePositionAdjustment = UIOffsetZero;

if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
_unselectedTitleAttributes = @{
NSFontAttributeName: [UIFont systemFontOfSize:12],
NSForegroundColorAttributeName: [UIColor blackColor],
Expand Down Expand Up @@ -126,7 +128,7 @@ - (void)drawRect:(CGRect)rect {
imageSize.width, imageSize.height)];
} else {

if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
titleSize = [_title boundingRectWithSize:CGSizeMake(frameSize.width, 20)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: titleAttributes[NSFontAttributeName]}
Expand Down Expand Up @@ -182,7 +184,7 @@ - (void)drawRect:(CGRect)rect {
if ([[self badgeValue] length]) {
CGSize badgeSize = CGSizeZero;

if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
badgeSize = [_badgeValue boundingRectWithSize:CGSizeMake(frameSize.width, 20)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: [self badgeTextFont]}
Expand Down Expand Up @@ -215,7 +217,7 @@ - (void)drawRect:(CGRect)rect {

CGContextSetFillColorWithColor(context, [[self badgeTextColor] CGColor]);

if ([[[UIDevice currentDevice] systemVersion] integerValue] >= 7.0) {
if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
NSMutableParagraphStyle *badgeTextStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
[badgeTextStyle setLineBreakMode:NSLineBreakByWordWrapping];
[badgeTextStyle setAlignment:NSTextAlignmentCenter];
Expand Down
Loading

0 comments on commit ea534a6

Please sign in to comment.