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

iOS 导航条(navigation)

2013-10-16 13:45 477 查看
1.初始化

-
(BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
 
  self.window = [[[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]]
autorelease];
 
  self.viewController = [[[ViewController alloc]
initWithNibName:@"ViewController" bundle:nil]
autorelease];

 
  UINavigationController *nav =
[[[UINavigationController alloc]
initWithRootViewController:self.viewController]
autorelease];
 
  nav.navigationBar.barStyle =
UIBarStyleBlack;
 
  self.window.rootViewController =
nav;

 
  [self.window makeKeyAndVisible];
 
  return YES;
}


2.更改导航条上返回按钮的标题

UIBarButtonItem *barButtonItem =
[[UIBarButtonItem alloc] init];
barButtonItem.title =
@"buttonName";
self.navigationItem.backBarButtonItem =
barButtonItem; 
[barButtonItem release]


3.返回到上一个视图,返回根视图以及返回到任意视图

//返回到上一个视图,比如用户单击导航栏的Back按钮
[[self navigationController]
popViewControllerAnimated:YES]
//返回到根视图
[[self navigationController]
popToRootViewControllerAnimated:YES];
//返回到任意视图
[[self navigationController]
popToViewController:destiationViewController
animated:YES];


4.添加自定义视图到导航条的标题栏

该代码添加一个自定义的UILabel,但也可以添加其他任意控件(或者视图),比如UIImageView,UISwitch等等。

self.navigationItem.titleView = [[[UILabel
alloc] 

   
  initWithFrame:CGRectMake(0.0f,0.0f, 120.0f,
36.0f)] autorelease];

self.title = @"Hello";



5.为导航添加新按钮



修改navigationItem可以为导航栏添加新按钮,如果要删除按钮,将该项指定为空值即可。下面代码是为导航栏添加右边按钮

self.navigationItem.rightBarButtonItem =
[[[UIBarButtonItem alloc] initWithTitle:@"Action"
style:UIBarButtonItemStylePlain target:self
action:@selector(performAction:)] autorelease];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: