您的位置:首页 > 移动开发 > IOS开发

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

2013-12-02 17:51 736 查看
虽然网上资源很多,但是还是很让人难受,尤其是新手。

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

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

最终实现效果:

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

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

#import "LeftWin.h"

@interface LeftWin ()

@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 setBackgroundColo
6fe6
r:[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios开发 界面 导航 ios
相关文章推荐