Navigation Bar Tutorial
From GameOver
Contents |
Introduction
This is a very basic navigation bar near the top of the screen that let's you toggle through the bar styles.
Screenshots
Requirements
This program requires the iPhone Toolchain to compile. You can view how to obtain and install it here.
Packaged Files
For your convenience this projects source code and compiled code is avaiable for download in the following formats: tar.bz2, zip
Source Code
README
All files contained herein are under the copyright notice and license contained below. It is omitted from each individually displayed file for redundancy purposes only.
DSNavigationBar - iPhone navigation bar example. Copyright (C) 2007 David Supuran <http://www.iphonegameover.com> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
DSNavigationBar.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIApplication.h>
@interface DSNavigationBar : UIApplication {
unsigned int _barStyle;
}
- (void) applicationDidFinishLaunching: (id) unused;
- (unsigned int) barStyle;
- (void) setBarStyle: (unsigned int) barStyle;
@end
DSNavigationBar.m
#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UITable.h>
#import <CoreGraphics/CGColor.h>
#import "DSNavigationBar.h"
@implementation DSNavigationBar
- (void) applicationDidFinishLaunching: (id) unused
{
struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
struct CGRect trect = [UIHardware fullScreenApplicationContentRect];
rect.origin.x = rect.origin.y = 0;
trect.size.height = trect.size.height / 2;
/* Initialize the main window */
UIWindow* window = [[UIWindow alloc] initWithContentRect: rect];
[window orderFront: self];
[window makeKey: self];
[window _setHidden: false];
/* Initialize the main view */
UIView* mainView = [[[UIView alloc] initWithFrame: rect] autorelease];
[window setContentView: mainView];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 40.0f)];
[navBar showButtonsWithLeftTitle: @"Exit" rightTitle: @"Change Bar Style" leftBack: true];
[navBar setBarStyle: 0];
[navBar setDelegate: self];
/* I don't care about this table, it is just made to be a place filler and color the screen white for the time being.
I have a feeling some people might not like "black" space below the navigation bar as it looks incomplete,
so for all purposes of this example you can ignore the following table.
*/
UITable *table = [[UITable alloc] initWithFrame: CGRectMake(0.0f, 40.0f, 320.0f, 480.0f - 40.0f)];
[mainView addSubview: navBar];
[mainView addSubview: table];
return;
}
- (void) navigationBar: (UINavigationBar*) navBar buttonClicked: (int) button
{
switch (button)
{
case 0:
NSLog(@"Old Style is: %d", [self barStyle]);
if ([self barStyle] == 2)
{
[self setBarStyle: 0];
}
else
{
[self setBarStyle: [self barStyle] + 1];
}
NSLog(@"New Style is: %d", [self barStyle]);
[navBar setBarStyle: [self barStyle]];
break;
case 1:
NSLog(@"Termination requested by user.");
[self terminate];
break;
default:
NSLog(@"Should never reach here");
}
}
- (unsigned int) barStyle
{
return _barStyle;
}
- (void) setBarStyle: (unsigned int) barStyle
{
_barStyle = barStyle;
}
@end
main.m
#import <UIKit/UIKit.h>
#import "DSNavigationBar.h"
int main(int argc, char **argv)
{
int ret;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
ret = UIApplicationMain(argc, argv, [DSNavigationBar class]);
[pool release];
return ret;
}
Makefile
NAME := DSNavigationBar
VERSION := 0.1
CC := arm-apple-darwin-gcc
CFLAGS := -Wall -pipe -ansi -O3
LD := ${CC}
LDFLAGS := -lobjc -framework CoreFoundation -framework Foundation -framework UIKit -framework LayerKit -framework CoreGraphics
all: ${NAME}
${NAME}: main.o ${NAME}.o
$(LD) $(LDFLAGS) -o $@ $^
%.o: %.m
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
clean:
rm -f *.o ${NAME}

