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

Commit

Permalink
* A much simpler way to change navigation and status bar styles
Browse files Browse the repository at this point in the history
* Fix some positioning issues with photo and thumb view controllers

Signed-off-by: Joakim Bodin <joakim.bodin@gmail.com>
  • Loading branch information
joehewitt authored and Joakim Bodin committed Apr 16, 2009
1 parent ef49393 commit cf81218
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 131 deletions.
2 changes: 1 addition & 1 deletion samples/TTCatalog/Classes/PhotoTest1Controller.m
Expand Up @@ -36,7 +36,7 @@ - (void)viewDidLoad {
caption:@"These are the wood tiles that we had installed after the accident."] autorelease],

// Causes album to be loaded
// [NSNull null],
// [NSNull null],

[[[MockPhoto alloc]
initWithURL:@"http://farm2.static.flickr.com/1124/3164979509_bcfdd72123.jpg?v=0"
Expand Down
20 changes: 10 additions & 10 deletions src/TTPhotoViewController.m
Expand Up @@ -39,6 +39,9 @@ - (id)init {
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:
TTLocalizedString(@"Photo", @"Title for back button that returns to photo browser")
style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
self.navigationBarStyle = UIBarStyleBlackTranslucent;
self.navigationBarTintColor = nil;
self.statusBarStyle = UIStatusBarStyleBlackTranslucent;
}
return self;
}
Expand Down Expand Up @@ -312,7 +315,7 @@ - (void)hideBarsAnimationDidStop {
- (void)loadView {
CGRect screenFrame = [UIScreen mainScreen].bounds;
self.view = [[[TTUnclippedView alloc] initWithFrame:screenFrame] autorelease];

CGRect innerFrame = CGRectMake(0, -CHROME_HEIGHT,
screenFrame.size.width, screenFrame.size.height + CHROME_HEIGHT);
_innerView = [[UIView alloc] initWithFrame:innerFrame];
Expand Down Expand Up @@ -340,18 +343,12 @@ - (void)loadView {

_toolbar = [[UIToolbar alloc] initWithFrame:
CGRectMake(0, screenFrame.size.height - TOOLBAR_HEIGHT, screenFrame.size.width, TOOLBAR_HEIGHT)];
_toolbar.barStyle = UIBarStyleBlackTranslucent;
_toolbar.barStyle = self.navigationBarStyle;
_toolbar.items = [NSArray arrayWithObjects:
space, _previousButton, space, _nextButton, space, nil];
[_innerView addSubview:_toolbar];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self changeNavigationBarStyle:UIBarStyleBlackTranslucent barColor:nil
statusBarStyle:UIStatusBarStyleBlackTranslucent];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];

Expand All @@ -377,11 +374,14 @@ - (void)viewWillDisappear:(BOOL)animated {
if (self.nextViewController) {
[self showBars:YES animated:NO];
}
[self restoreNavigationBarStyle];
}

- (void)showBars:(BOOL)show animated:(BOOL)animated {
[super showBars:show animated:animated];

CGFloat alpha = show ? 1 : 0;
if (alpha == _toolbar.alpha)
return;

if (animated) {
[UIView beginAnimations:nil context:nil];
Expand All @@ -402,7 +402,7 @@ - (void)showBars:(BOOL)show animated:(BOOL)animated {

[self showCaptions:show];

_toolbar.alpha = show ? 1 : 0;
_toolbar.alpha = alpha;

if (animated) {
[UIView commitAnimations];
Expand Down
35 changes: 18 additions & 17 deletions src/TTThumbsViewController.m
Expand Up @@ -198,6 +198,9 @@ - (id)init {
_photoSource = nil;

self.hidesBottomBarWhenPushed = YES;
self.navigationBarStyle = UIBarStyleBlackTranslucent;
self.navigationBarTintColor = nil;
self.statusBarStyle = UIStatusBarStyleBlackTranslucent;
}

return self;
Expand All @@ -213,46 +216,44 @@ - (void)dealloc {
// UIViewController

- (void)loadView {
self.view = [[[TTUnclippedView alloc] initWithFrame:TTApplicationFrame()] autorelease];
self.view.backgroundColor = [UIColor whiteColor];
CGRect screenFrame = [UIScreen mainScreen].bounds;
self.view = [[[TTUnclippedView alloc] initWithFrame:screenFrame] autorelease];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

CGRect innerFrame = CGRectMake(0, -CHROME_HEIGHT,
screenFrame.size.width, screenFrame.size.height + CHROME_HEIGHT);
UIView* innerView = [[UIView alloc] initWithFrame:innerFrame];
innerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:innerView];

self.tableView = [[[UITableView alloc] initWithFrame:TTNavigationFrame()
CGRect tableFrame = CGRectMake(0, CHROME_HEIGHT,
screenFrame.size.width, screenFrame.size.height - CHROME_HEIGHT);
self.tableView = [[[UITableView alloc] initWithFrame:tableFrame
style:UITableViewStylePlain] autorelease];
self.tableView.rowHeight = kThumbnailRowHeight;
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleHeight;
| UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.separatorColor = [UIColor whiteColor];
self.tableView.contentInset = UIEdgeInsetsMake(4, 0, 0, 0);
self.tableView.clipsToBounds = NO;
[self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self changeNavigationBarStyle:UIBarStyleBlackTranslucent barColor:nil
statusBarStyle:UIStatusBarStyleBlackTranslucent];
[innerView addSubview:self.tableView];
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self suspendLoadingThumbnails:NO];

if (animated && !self.nextViewController) {
if (!self.nextViewController) {
self.view.superview.frame = CGRectOffset(self.view.superview.frame, 0, TOOLBAR_HEIGHT);
}
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

if (animated) {
self.view.superview.frame = CGRectOffset(self.view.superview.frame, 0, TOOLBAR_HEIGHT);
}

[self restoreNavigationBarStyle];
self.view.superview.frame = CGRectOffset(self.view.superview.frame, 0, TOOLBAR_HEIGHT);
}

- (void)viewDidDisappear:(BOOL)animated {
Expand Down
6 changes: 5 additions & 1 deletion src/TTUnclippedView.m
Expand Up @@ -6,8 +6,12 @@ - (void)didMoveToSuperview {
for (UIView* p = self; p; p = p.superview) {
// This allows the view to be shown "full screen", and not offset by the toolbar and status bar
p.clipsToBounds = NO;
p.backgroundColor = self.backgroundColor;
//p.backgroundColor = self.backgroundColor;
}
}

- (void)setFrame:(CGRect)rect {
[super setFrame:rect];
}

@end
101 changes: 17 additions & 84 deletions src/TTViewController.m
@@ -1,49 +1,21 @@
#import "Three20/TTViewController.h"
#import "Three20/TTErrorView.h"
#import "Three20/TTURLRequestQueue.h"
#import "Three20/TTStyleSheet.h"

///////////////////////////////////////////////////////////////////////////////////////////////////

@implementation TTViewController

@synthesize frozenState = _frozenState, viewState = _viewState,
contentError = _contentError, appearing = _appearing, appeared = _appeared,
contentError = _contentError, navigationBarStyle = _navigationBarStyle,
navigationBarTintColor = _navigationBarTintColor, statusBarStyle = _statusBarStyle,
appearing = _appearing, appeared = _appeared,
autoresizesForKeyboard = _autoresizesForKeyboard;

///////////////////////////////////////////////////////////////////////////////////////////////////
// private

//- (void)changeStyleFrom:(TTViewControllerStyle)from {
// if (from != style) {
// UINavigationBar* bar = self.navigationController.navigationBar;
// if (style == TTViewControllerStyleTranslucent) {
// bar.tintColor = nil;
// bar.barStyle = UIBarStyleBlackTranslucent;
// [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent
// animated:YES];
// } else {
// bar.tintColor = [TTResources facebookDarkBlue];
// bar.barStyle = UIBarStyleDefault;
// [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault
// animated:YES];
// }
// }
//}
//
//- (void)updateStyle {
// TTViewController* topController = (TTViewController*)self.navigationController.topViewController;
// if (topController != self) {
// [self changeStyleFrom:topController.style];
// } else {
// NSArray* controllers = self.navigationController.viewControllers;
// if (controllers.count > 1) {
// TTViewController* backController = [controllers objectAtIndex:controllers.count-2];
// [self changeStyleFrom:backController.style];
// }
// }
// [topController release];
//}

- (BOOL)resizeForKeyboard:(NSNotification*)notification {
NSValue* v1 = [notification.userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect keyboardBounds;
Expand Down Expand Up @@ -87,10 +59,9 @@ - (id)init {
_frozenState = nil;
_viewState = TTViewEmpty;
_contentError = nil;
_previousBar = nil;
_previousBarStyle = 0;
_previousBarTintColor = nil;
_previousStatusBarStyle = 0;
_navigationBarStyle = UIBarStyleDefault;
_navigationBarTintColor = nil;
_statusBarStyle = UIStatusBarStyleDefault;
_invalidView = YES;
_invalidViewLoading = NO;
_invalidViewData = YES;
Expand All @@ -99,6 +70,8 @@ - (id)init {
_appeared = NO;
_unloaded = NO;
_autoresizesForKeyboard = NO;

self.navigationBarTintColor = TTSTYLEVAR(navigationBarTintColor);
}
return self;
}
Expand All @@ -114,8 +87,7 @@ - (void)dealloc {

[[TTURLRequestQueue mainQueue] cancelRequestsWithDelegate:self];

[_previousBar release];
[_previousBarTintColor release];
[_navigationBarTintColor release];
[_frozenState release];
[_contentError release];
[self unloadView];
Expand Down Expand Up @@ -152,6 +124,12 @@ - (void)viewWillAppear:(BOOL)animated {
[self validateView];

[TTURLRequestQueue mainQueue].suspended = YES;

UINavigationBar* bar = self.navigationController.navigationBar;
bar.tintColor = _navigationBarTintColor;
bar.barStyle = _navigationBarStyle;

[[UIApplication sharedApplication] setStatusBarStyle:_statusBarStyle animated:YES];
}

- (void)viewDidAppear:(BOOL)animated {
Expand All @@ -162,10 +140,6 @@ - (void)viewWillDisappear:(BOOL)animated {
_appearing = NO;
}

//- (void)viewDidDisappear:(BOOL)animated {
// _appearing = NO;
//}

- (void)didReceiveMemoryWarning {
TTLOG(@"MEMORY WARNING FOR %@", self);

Expand Down Expand Up @@ -274,7 +248,7 @@ - (void)invalidateViewState:(TTViewState)state {
_invalidViewLoading = (_viewState & TTViewLoadingStates) != (state & TTViewLoadingStates);
}
if (!_invalidViewData) {
_invalidViewData = state == TTViewDataLoaded
_invalidViewData = state == TTViewDataLoaded || state == TTViewEmpty
|| (_viewState & TTViewDataStates) != (state & TTViewDataStates);
}

Expand Down Expand Up @@ -369,45 +343,4 @@ - (NSString*)subtitleForError:(NSError*)error {
return TTLocalizedString(@"Sorry, an error has occurred.", @"");
}

- (void)changeNavigationBarStyle:(UIBarStyle)barStyle barColor:(UIColor*)barColor
statusBarStyle:(UIStatusBarStyle)statusBarStyle {
if (!_previousBar) {
UINavigationBar* bar = self.navigationController.navigationBar;
if (!self.nextViewController) {
[_previousBar release];
[_previousBarTintColor release];
_previousBar = [bar retain];
_previousBarStyle = bar.barStyle;
_previousBarTintColor = [bar.tintColor retain];
_previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
}

bar.tintColor = barColor;
bar.barStyle = barStyle;

[[UIApplication sharedApplication] setStatusBarStyle:statusBarStyle animated:YES];
}
}

- (void)restoreNavigationBarStyle {
// If we're going backwards...
if (!self.nextViewController && _previousBar) {
_previousBar.tintColor = _previousBarTintColor;
_previousBar.barStyle = _previousBarStyle;

UIApplication* app = [UIApplication sharedApplication];
if (app.statusBarHidden) {
app.statusBarStyle = _previousStatusBarStyle;
[app setStatusBarHidden:NO animated:YES];
} else {
[app setStatusBarStyle:_previousStatusBarStyle animated:YES];
}

[_previousBarTintColor release];
_previousBarTintColor = nil;
[_previousBar release];
_previousBar = nil;
}
}

@end
33 changes: 18 additions & 15 deletions src/Three20/TTViewController.h
Expand Up @@ -18,10 +18,9 @@ typedef enum {
TTViewState _viewState;
NSError* _contentError;

UINavigationBar* _previousBar;
UIBarStyle _previousBarStyle;
UIColor* _previousBarTintColor;
UIStatusBarStyle _previousStatusBarStyle;
UIBarStyle _navigationBarStyle;
UIColor* _navigationBarTintColor;
UIStatusBarStyle _statusBarStyle;

BOOL _invalidView;
BOOL _invalidViewLoading;
Expand Down Expand Up @@ -58,6 +57,21 @@ typedef enum {
*/
@property(nonatomic, retain) NSError* contentError;

/**
* The style of the navigation bar when this controller is pushed onto a navigation controller.
*/
@property(nonatomic) UIBarStyle navigationBarStyle;

/**
* The color of the navigation bar when this controller is pushed onto a navigation controller.
*/
@property(nonatomic,retain) UIColor* navigationBarTintColor;

/**
* The style of the status bar when this controller is appearing.
*/
@property(nonatomic) UIStatusBarStyle statusBarStyle;

/**
* The view has appeared at least once.
*/
Expand Down Expand Up @@ -201,15 +215,4 @@ typedef enum {
*/
- (NSString*)subtitleForError:(NSError*)error;

/**
*
*/
- (void)changeNavigationBarStyle:(UIBarStyle)barStyle barColor:(UIColor*)barColor
statusBarStyle:(UIStatusBarStyle)statusBarStyle;

/**
*
*/
- (void)restoreNavigationBarStyle;

@end
6 changes: 3 additions & 3 deletions src/UIViewControllerAdditions.m
Expand Up @@ -81,11 +81,11 @@ - (void)pushViewController:(UIViewController*)controller

[self pushViewController:controller animated:NO];

[UIView beginAnimations:nil context:NULL];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:TT_FLIP_TRANSITION_DURATION];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
[UIView setAnimationTransition:transition forView:self.view.window cache:YES];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[UIView commitAnimations];
}

Expand All @@ -98,7 +98,7 @@ - (void)popViewControllerAnimatedWithTransition:(UIViewAnimationTransition)trans
[UIView setAnimationDuration:TT_FLIP_TRANSITION_DURATION];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(pushAnimationDidStop)];
[UIView setAnimationTransition:transition forView:self.view.window cache:YES];
[UIView setAnimationTransition:transition forView:self.view cache:YES];
[UIView commitAnimations];
}

Expand Down

0 comments on commit cf81218

Please sign in to comment.