1. 程式人生 > >IOS導航欄的簡單用法以及自定義實現例程

IOS導航欄的簡單用法以及自定義實現例程

雖然網上資源很多,但是還是很讓人難受,尤其是新手。

找了好久,好多例程都看了,有的不能跳轉,有的不能新增按鈕,當然這是自己能力有限所致,看得越多,越糊塗了。

最終找到了比較合適的解決方案,記下也分享一下!

最終實現效果:

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"

@interfaceIndexWin ()

@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_leftwin animated:YES];

}

3、自定義的跳轉後的介面[LeftWin],其他函式是系統預設的

#import "LeftWin.h"

@interfaceLeftWin ()

@end

@implementation LeftWin

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

{

   self = [superinitWithNibName:nibNameOrNil bundle: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