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;
}
#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];
}
#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- IOS导航栏的简单用法以及自定义实现例程
- IOS 实现自定义的导航栏背景以及自定义颜色的状态栏
- IOS 实现自定义的导航栏背景以及自定义颜色的状态栏(支持7.0以及低版本)
- IOS 实现自定义的导航栏背景以及自定义颜色的状态栏(支持7.0以及低版本)
- IOS 实现自定义的导航栏背景以及自定义颜色的状态栏(支持7.0以及低版本)
- iOS实现自定义导航
- Android开发--自定义动态导航栏的实现(很简单)
- IOS中左右两侧导航栏的简单实现
- IOS中左右两侧导航栏的简单实现
- IOS中左右两侧导航栏的简单实现
- 导航栏的简单实现
- iOS自定义导航栏
- iOS自定义导航栏
- iOS 自定义导航栏
- iOS自定义导航栏的返回按钮
- iOS 自定义导航栏的返回按钮
- iOS系统导航栏的自定义化
- IOS 导航栏的 一些用法
- 单总体分布卡方拟合优度检验
- Android开发学习之常见适配器总结
- JSTL标签引入
- CSS伪类
- mysql 预处理
- IOS导航栏的简单用法以及自定义实现例程
- Unable to cast object of type 'System.Web.HttpApplication' to type 'Microsoft.Practices.CompositeWeb
- 51单片机中的定时器和计数器
- (Java)统计随机产生的字母各自出现次数
- 两分类变量相互独立的检验
- C语言switch语句的用法详解
- 愁秋渲染的离殇
- 最短路径算法(Shortest-path Algorithms)
- Ubuntu1204安装adobe flash player插件