iOS UINavigationController总结

来源:互联网 发布:全球离婚率数据 编辑:程序博客网 时间:2024/05/17 20:02
iOS UINavigationController总结
UINavigationController通过栈来实现。添加一个Controller为入栈,释放一个Controller为出栈。复习下栈:
1。栈是先进后出
2。栈顶是最后一个入栈的对象
3。基栈是是第一个入栈的对象(栈底)

UINavigationController经常使用的函数:
(1)- (id)initWithRootViewController:(UIViewController *)rootViewController
添加根视图控制器,最先显示。
(2)- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
Controller入栈操作
(3)- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
Controller出栈操作
这里要注意:每次push Controller后,Controller系统返回按钮实现pop这个Controller。

UINavigationBar按钮
(1)用户自定义按钮

//定义UINavigationBar左右按钮

self.iCityBarButton = [[UIBarButtonItem alloc] initWithTitle:@"北京"style:UIBarButtonItemStylePlain target:self action:@selector(selectCityButtonPressed)];

 

self.navigationItem.leftBarButtonItem = self.iCityBarButton;

 

self.navigationItem.rightBarButtonItem = self.iCityBarButton;

//定义系统样式的返回按钮
1。返回按钮title为“返回”,方法1:
假如有2个Controller,为A,B。
如果从A push 到B,可以在push的函数中self.B.title = @"返回"。
在B的viewWillAppear中再把title改回来。不过我不推荐这种方法,感觉很麻烦。
2。

   //修改下一个controller后退按钮名称

    self.navigationItem.backBarButtonItem= [[UIBarButtonItem alloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlain target:self action:nil];

感觉还是这种方法比较好。

(2)UINavigationBar颜色设置
1。Bar的背景设置
思路:通过自定义UINavigationBar的类别,重画UINavigationBar,将一张picture画上去。
具体做法:
定义一个UINavigationBar类别,重写draw方法绘制picture。
在类别中定义class类,返回类别名称。
定义类别名称类,在draw方法中调用UINavigationBar的绘制方法。
代码如下:

#import <UIKit/UIKit.h>

@interface MyUINavigationBar : UINavigationBar

- (void) drawRect:(CGRect)rect;

@end

@interface UINavigationBar (LazyNavigationBar)

- (void)drawRect:(CGRect)rect;

@end

 

#import "MyUINavigationBar.h"

@implementation MyUINavigationBar

- (void)drawRect:(CGRect)rect 

{

    [super drawRect:rect];

}

@end

@implementation UINavigationBar (LazyNavigationBar)

+ (Class)class 

{

    //使用NSClassFromString进行不确定的类的初始化

    return NSClassFromString(@"MyUINavigationBar");

}

-(void)drawRect:(CGRect)rect 

{

    UIImage *backImage = [UIImage imageNamed:@"bg_1x44.png"];

    [backImage drawInRect:CGRectMake(00self.frame.size.widthself.frame.size.height)];

}

@end

实际上在SDK5.0之后,有个方法一句话搞定,不过在4.3模拟器下是不支持的。
代码如下:
    [self.navigationController.navigationBar setBackgroundImage: image forBarMetrics:UIBarMetricsDefault];
2.Bar上按钮颜色设置
  (1)[[UIBarButtonItem alloc] initWithCustomView:Button];
这种方法可以自定义按钮,如果需求要求做系统的,就别用这个方法了,考验美工。
(2)    [self.navigationController.navigationBar setTintColor:[UIColor colorWithRed:(float)1/255green:(float)98/255 blue:(float)161/255 alpha:1.0]];
原创粉丝点击