IOS导航栏的简单用法以及自定义实现例程

来源:互联网 发布:哥伦比亚统计学 知乎 编辑:程序博客网 时间:2024/05/22 15:59

虽然网上资源很多,但是还是很让人难受,尤其是新手。

找了好久,好多例程都看了,有的不能跳转,有的不能添加按钮,当然这是自己能力有限所致,看得越多,越糊涂了。

最终找到了比较合适的解决方案,记下也分享一下!

最终实现效果:

1、主界面

主界面

2、跳转后页面

跳转后界面

上代码:

1、代理实现部分,其他函数是系统默认的

#import "AppDelegate.h"

#import "IndexWin.h"

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];

    // Override point for customization after application launch.

    

    //创建主窗口

   IndexWin *m_indexWin=[[IndexWinalloc]initWithNibName:@"IndexWin"bundle:nil];

    //依附于创建的主窗口创建导航栏控制器

    UINavigationController *um_indexWin=[[UINavigationControlleralloc]initWithRootViewController:m_indexWin];

    //设置导航栏渐变颜色

    [um_indexWin.navigationBarsetTintColor:[UIColorblackColor]];

    //隐藏导航栏,便于自定义导航栏

    um_indexWin.navigationBarHidden=YES;

    //设置窗口的根视图控制器

   self.window.rootViewController=um_indexWin;

    //设置窗口背景色

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    return YES;

}

2、自定义的主窗口[IndexWin],其他函数是系统默认的

#import "IndexWin.h"


@interface IndexWin ()


@end

@implementation IndexWin


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    

    //设置导航栏标题

    [selfsetTitle:@"Simple"];

    //自定义导航栏按钮

   UIButton *um_left=[[UIButtonalloc]initWithFrame:CGRectMake(20,8,50, 30)];

    //为导航栏按钮添加事件

    [um_left addTarget:selfaction:@selector(af_go:)forControlEvents:UIControlEventTouchUpInside];

    //设置按钮标题

    [um_left setTitle:@"Go"forState:UIControlStateNormal];

    //设置按钮背景色

    [um_left setBackgroundColor:[UIColorgrayColor]];

    //自定义导航栏容器视图

   UIView *um_navBar=[[UIViewalloc]initWithFrame:CGRectMake(0,0,320, 44)];

    //自定义导航栏添加按钮

    [um_navBaraddSubview:um_left];

    //自定义导航栏背景色

    [um_navBar setBackgroundColor:[UIColorgrayColor]];

    //自定义导航栏透明度

    [um_navBarsetAlpha:0.6];

    //添加自定义导航栏到当前窗口

    [self.viewaddSubview:um_navBar];

    

}

-(void)viewWillAppear:(BOOL)animated

{

    //在导航栏添加按钮,此时导航栏是系统默认的,貌似每次界面隐藏都需要重新添加[不确定]

//    UIBarButtonItem *m_rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(af_go:)];

//    UIBarButtonItem *m_leftBtn=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(af_go:)];

//    [self.navigationItem setLeftBarButtonItem:m_leftBtn animated:YES];

//    [self.navigationItem setRightBarButtonItem:m_rightBtn animated:YES];

    //        self.navigationController.navigationBarHidden=YES;


}

-(IBAction)af_go:(id)sender

{

    //创建将要跳转到的视图

    LeftWin *m_leftwin=[[LeftWinalloc]initWithNibName:@"LeftWin"bundle:nil];

    //利用导航栏的push方法进行页面跳转

    [self.navigationControllerpushViewController:m_leftwinanimated:YES];

}


3、自定义的跳转后的界面[LeftWin],其他函数是系统默认的

#import "LeftWin.h"


@interface LeftWin ()


@end


@implementation LeftWin


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    

   UIButton *um_left=[[UIButtonalloc]initWithFrame:CGRectMake(20,8,50, 30)];

    [um_left addTarget:selfaction:@selector(Back:)forControlEvents:UIControlEventTouchUpInside];

    [um_left setTitle:@"Go"forState:UIControlStateNormal];

    [um_left setBackgroundColor:[UIColorgrayColor]];

    

   UIView *um_navBar=[[UIViewalloc]initWithFrame:CGRectMake(0,0,320, 44)];

    [um_navBaraddSubview:um_left];

    [um_navBar setBackgroundColor:[UIColorgrayColor]];

    [um_navBarsetAlpha:0.6];

    

    [self.viewaddSubview:um_navBar];

}

-(IBAction)Back:(id)sender

{

    //跳转回上一级窗口

    [self.navigationControllerpopViewControllerAnimated:YES];

}



附:源码

http://download.csdn.net/detail/hg_lin/6644981