iOS之UINavigationBar的使用

纯代码实现

1、AppDelegate类

//.h
#import <UIKit/UIKit.h>
@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController* viewController;
@end


//.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc]init];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

2、FKViewController类

#import "FKViewController.h"

@interface FKViewController ()

@end

@implementation FKViewController
// 记录当前是添加第几个UINavigationItem的计数器
NSInteger count;
UINavigationBar *navigationBar;
- (void)viewDidLoad
{
	[super viewDidLoad];
	count = 1;
	// 创建一个导航栏
   	navigationBar = [[UINavigationBar alloc]
		initWithFrame:CGRectMake(0, 20, 320, 44)];
	// 把导航栏添加到视图中
	[self.view addSubview:navigationBar];
	// 调用push方法添加一个UINavigationItem
	[self push];
}

-(void)push
{
	// 把导航栏集合添加入导航栏中,设置动画打开
	[navigationBar pushNavigationItem:
	 	[self makeNavItem] animated:YES];
	count++;	
}

-(void)pop
{
	// 如果还有超过2个的UINavigationItem
	if(count > 2)
	{
		count--;
		// 弹出最顶层的UINavigationItem
		[navigationBar popNavigationItemAnimated:YES];
	}
	else
	{
		// 使用UIAlertView提示用户
		UIAlertView* alert = [[UIAlertView alloc]
			initWithTitle:@"提示"
			message:@"只剩下最后一个导航项,再出栈就没有了"
			delegate:nil cancelButtonTitle:@"OK"
			otherButtonTitles: nil];
		[alert show];
	}
}

- (UINavigationItem*) makeNavItem
{
	// 创建一个导航项
	UINavigationItem *navigationItem = [[UINavigationItem alloc]
			initWithTitle:nil];
	// 创建一个左边按钮
	UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]
		initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
		target:self
		action:@selector(push)];
	// 创建一个右边按钮
	UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
		initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
		target:self
		action:@selector(pop)];
	//设置导航栏内容
	navigationItem.title = [NSString stringWithFormat:
		@"第【%ld】个导航项"
		, count];
	//把左右两个按钮添加入导航栏集合中
	[navigationItem setLeftBarButtonItem:leftButton];
	[navigationItem setRightBarButtonItem:rightButton];
    
    switch(count%4)
    {
        case 0:
            [self.view setBackgroundColor:[UIColor redColor]];
            break;
        case 1:
            [self.view setBackgroundColor:[UIColor orangeColor]];
            break;
        case 2:
            [self.view setBackgroundColor:[UIColor blueColor]];
            break;
        case 3:
            [self.view setBackgroundColor:[UIColor yellowColor]];
            break;
        default:
            break;
    }

	return navigationItem;
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值