您的位置:首页 > 产品设计 > UI/UE

UINavigationBar & UINavigationItem

2015-08-11 22:17 274 查看
如果在一个UINavigationController容器类里面push一个UIViewController的话,nagationBarItem是很好处理的,因为root根窗口提供了一个导航栏,用个自定义的样式按钮可以直接去替换它,并且iOS提供了丰富的使用API去使用。

但是如果在一个UITabBarController(非UINavigationController类)里面直接初始化一个UIViewController并用push它,达不到push的美妙效果(很显然),更令人头痛的是,连导航栏都是空的,那功能十分好用的导航栏按钮就更是谈不上了。

这个时候我们可以手动添加UIViewController的navigationBar(只是加上了导航栏,push问题仍然解决不了哈)

//添加了导航栏
    CGRect rect = [[UIScreen mainScreen] bounds];
    UINavigationBar* navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 64)];
    
    //修改tint颜色,方法是宏定义的
    navigationBar.tintColor = UIColorWithRGB(200, 100, 162);
    
    //设置NavigationItem
    UINavigationItem* navigationBarTitle = [[UINavigationItem alloc] initWithTitle:@"Delegation Test"];
    [navigationBar pushNavigationItem:navigationBarTitle animated:YES];
    [self.view addSubview:navigationBar];
    
    //设置了系统的UIBarButtonItem样式
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(navigationItemCancel:)];
    navigationBarTitle.leftBarButtonItem = item;
    [navigationBar setItems:[NSArray arrayWithObject:navigationBarTitle]];


如果想将UIBarButtonItem设置成自定义的,可以:

第一种方法:用图片上填充

UIImage* searchimage = [UIImage imageNamed:@"search.png"];
    UIBarButtonItem* barbtn = [[UIBarButtonItem alloc] initWithImage:nil style:UIBarButtonItemStyleDone target:self action:@selector(searchprogram)];
    barbtn.image = searchimage;
    self.navigationItem.rightBarButtonItem = barbtn;


第二种方法:自定义UIButton来替代

UIButton* rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
    [rightButton setImage:[UIImage imageNamed:@"search.png"]
                 forState:UIControlStateNormal];
    [rightButtonaddTarget:self action:@selector(searchprogram)
         forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem= rightItem;


第三种方法:用系统提供的样式

UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(methodtocall:) ];


如果想要它隐藏呢:

self.navigationItem.rightBarButtonItem = nil;


参考至:

超详细总结:http://blog.csdn.net/zhibudefeng/article/details/7656311

添加导航栏:http://blog.sina.com.cn/s/blog_8f5097be0101ath7.html

修改barButton: http://blog.csdn.net/zhuzhihai1988/article/details/7701998
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: