现在的位置: 首页 > 综合 > 正文

iOS UINavigationController总结

2019年08月27日 ⁄ 综合 ⁄ 共 2302字 ⁄ 字号 评论关闭

UINavigationController通过栈来实现。添加一个Controller为入栈,释放一个Controller为出栈。复习下栈:

1。栈是先进后出
2。栈顶是最后一个入栈的对象
3。基栈是是第一个入栈的对象(栈底)
UINavigationController经常使用的函数:
(1)- (id)initWithRootViewController:(UIViewController *)rootViewController
添加根视图控制器,最先显示。
(2)- (void)pushViewController:(UIViewController *)viewControlleranimated:(BOOL)animated;
Controller入栈操作
(3)- (UIViewController*)popViewControllerAnimated:(BOOL)animated;
Controller出栈操作
这里要注意:每次pushController后,Controller系统返回按钮实现pop这个Controller。
UINavigationBar按钮
(1)用户自定义按钮

//定义UINavigationBar左右按钮

self.iCityBarButton = [[UIBarButtonItemalloc] 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= [[UIBarButtonItemalloc]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"

@implementationMyUINavigationBar

- (void)drawRect:(CGRect)rect 

{

   [superdrawRect:rect];

}

@end

@implementationUINavigationBar (LazyNavigationBar)

+ (Class)class 

{

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

   returnNSClassFromString(@"MyUINavigationBar");

}

-(void)drawRect:(CGRect)rect 

{

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

    [backImagedrawInRect:CGRectMake(0,
0, self.frame.size.width,
self.frame.size.height)];

}

@end

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

抱歉!评论已关闭.