zoukankan      html  css  js  c++  java
  • 导航

     

     

    【UINavigationController】

    导航控制器

     

    步骤

    1,初始化

    2,push 进入新视图

    3,pop 返回

     

     

    返回到指定视图

    //通过导航控制器找到所有的界面

    NSArray *controllers =self.navigationController.viewControllers;

    //返回到固定界面 (前提是要返回到的视图控制器对象存在)

    [self.navigationController popToViewController:[controllers objectAtIndex:1] animated:YES];

     

     

    【self.navigationController.navigationBar】

    //[UINavigationBar appearance]这种方式也能找到navigationBar导航条。

    【导航条的设置是针对整个导航的,会影响当前导航中的所有页面】

     

    //导航条的隐藏状态

    //self.navigationController.navigationBarHidden = YES;

    @property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;

    - (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;

     

    //导航条的背景色

    @property(nonatomic,retain) UIColor *barTintColor;

    //内容渲染颜色

    @property(nonatomic,retain) UIColor *tintColor;

    //是否半透明(这个设置对起始坐标点{0,0}的影响权限最高)

    @property(nonatomic,assign,getter=isTranslucent) BOOL translucent;

    //背景图

    - (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIBarMetrics)barMetrics;

     

     

     

    【UIBarButtonItem】

    //导航条专用按钮

     

    //四种创建方式

    - (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;

    - (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action;

    - (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action;

    - (id)initWithCustomView:(UIView *)customView;

     

    //UIBarButtonSystemItemFlexibleSpace,是自带的一种空的item,可以当作占位符用

     

     

    【self.navigationItem】

    //给导航条设置专用按钮和titleView

     

    //title

    @property(nonatomic,copy) NSString *title;

    //中间的view

    @property(nonatomic,retain) UIView *titleView;

     

    //隐藏自带的返回按钮

    @property(nonatomic,assign) BOOL hidesBackButton;

    - (void)setHidesBackButton:(BOOL)hidesBackButton animated:(BOOL)animated;

     

    //左边和右边的专用按钮

    @property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;

    @property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

    - (void)setLeftBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;

    - (void)setRightBarButtonItem:(UIBarButtonItem *)item animated:(BOOL)animated;

     

     

    //左边和右边的专用按钮组(数组中存放的是UIBarButtonItem)

    @property(nonatomic,copy) NSArray *leftBarButtonItems;

    @property(nonatomic,copy) NSArray *rightBarButtonItems;

    - (void)setLeftBarButtonItems:(NSArray *)items animated:(BOOL)animated; 

    - (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated;

     

     

    【self.navigationController.toolbar】工具条

     

    //每个导航控制器都自带有一个工具栏,是一个UIToolBar的对象,显示在最底部,尺寸(320*44),工具栏默认处于隐藏状态

    //注意:自带的这个工具条一般情况下都不用。如果要用的话我们会自己创建一个新的UIToolBar

     

    //工具条隐藏状态

    //self.navigationController.toolbarHidden = NO;

    @property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden;

    - (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated;

     

    //背景色

    @property(nonatomic,retain) UIColor *barTintColor;

    //内容渲染颜色

    @property(nonatomic,retain) UIColor *tintColor;

    //是否半透明

    @property(nonatomic,assign,getter=isTranslucent) BOOL translucent;

    【上面的属性和navigationBar的使用方式一样】

     

    //设置背景图片

    - (void)setBackgroundImage:(UIImage *)backgroundImage forToolbarPosition:(UIBarPosition)topOrBottom barMetrics:(UIBarMetrics)barMetrics;

    //UIBarPosition中的枚举值只支持iOS7

    //UIToolbarPositionBottom(在iOS7和7以前的操作系统都适用)

    //UIToolbarPositionBottom 理解(每个UIView都可以指定TopBar 和BottomBar),导航控制器中工具栏是作为导航控制器view的BottomBar被自动创建出来的

     

    给toolBar设置专用button(数组中也是UIBarButtonItem)

    //让viewController直接设置

    //self.toolbarItems = array;

    @property (nonatomic, retain) NSArray *toolbarItems;

    - (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated;

     

    #import "AppDelegate.h"

    #import "FirstViewController.h"

     

    @implementation AppDelegate

     

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    {

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Override point for customization after application launch.

        

        //创建一个页面

        FirstViewController *fvc = [[FirstViewController alloc] init];

        

        //使用一个页面创建一个导航控制器

        UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:fvc];

        

        //将导航控制器和window关联

        self.window.rootViewController = nc;

        

        self.window.backgroundColor = [UIColor whiteColor];

        [self.window makeKeyAndVisible];

        return YES;

    }

     

    #import "FirstViewController.h"

    #import "SecondViewController.h"

     

    @interface FirstViewController ()

     

    @end

     

    @implementation FirstViewController

     

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        SecondViewController *svc = [[SecondViewController alloc] init];

        

        //通过页面找到nc,让nc推出一个新的页面

        [self.navigationController pushViewController:svc animated:YES];

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        self.view.backgroundColor = [UIColor redColor];

        

        [self setNavigationBar];

    }

     

    - (void)setNavigationBar

    {

        //设置导航条的隐藏状态(导航条是属于导航控制器的)

        self.navigationController.navigationBarHidden = NO;

        

        //通过页面找到nc,通过nc找到导航条

        //对导航条的设置是针对整个nc的

        //self.navigationController.navigationBar

        

        //设置背景色

        self.navigationController.navigationBar.barTintColor = [UIColor brownColor];

        

        //设置导航条内容(比如说自带的返回按钮)的渲染色

        self.navigationController.navigationBar.tintColor = [UIColor purpleColor];

        

        //设置半透明状态,如果写成NO,那么坐标{0,0}的位置是导航条的左下角

        //如果写成YES,坐标{0,0}的位置在左上角

        //在iOS7以前状态栏的高度是20,导航条高度是44,分开计算的

        //从iOS7开始,合并计算高度为64

        self.navigationController.navigationBar.translucent = NO;

        

        //设置背景图片,如果图片的大小是320*44(640*88)就会显示成iOS7之前风格

        //如果图片的大小不是320*44(640*88),就会用新风格并且平铺

        //在主流的设计中,图片的大小需要和导航条匹配(320*64)

        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"aa"] forBarMetrics:UIBarMetricsDefault];

        

        //设置整个导航中,导航条上title的文字属性,属性是通过字典来设置的

        //字典里放的就是文字属性的键值对

        self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:10], NSFontAttributeName, [UIColor greenColor], NSForegroundColorAttributeName, nil];

    }

     

    #import "SecondViewController.h"

    #import "ThirdViewController.h"

     

    @interface SecondViewController ()

     

    @end

     

    @implementation SecondViewController

     

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        ThirdViewController *tvc = [[ThirdViewController alloc] init];

        

        [self.navigationController pushViewController:tvc animated:YES];

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        self.view.backgroundColor = [UIColor orangeColor];

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame = CGRectMake(60, 70, 100, 40);

        btn.backgroundColor = [UIColor grayColor];

        [btn setTitle:@"返回" forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

        

        [self setNavigationItem];

    }

     

    - (void)setNavigationItem

    {

        //设置显示在导航条上的title,针对于当前页面的

        //下一个页面自带的返回按钮上的文字就是当前页的title

        self.navigationItem.title = @"oyeah~";

        

        //设置当前页面显示在导航条上的元素

        //self.navigationItem

        

        UILabel *tView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];

        tView.backgroundColor = [UIColor magentaColor];

        tView.text = @"gogo";

        tView.textAlignment = NSTextAlignmentCenter;

        

        //设置当前页面在导航条中间显示的view

        self.navigationItem.titleView = tView;

        

        //隐藏自带的返回按钮

        self.navigationItem.hidesBackButton = YES;

        

        /**********下面是专用按钮的四种创建方法*************/

        

        //1,使用文字创建一个专用按钮

        UIBarButtonItem *abbi = [[UIBarButtonItem alloc] initWithTitle:@"ab" style:UIBarButtonItemStyleDone target:self action:@selector(click)];

        

        //设置当前页面中显示在导航条左边的专用按钮

        self.navigationItem.leftBarButtonItem = abbi;

        

        //设置专用按钮上的文字属性(这个设置只对abbi生效)

        [abbi setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:30], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

        

        //2,使用图片创建专用按钮(只取图片的形状,不取颜色)

        UIBarButtonItem *bbbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"logo_title"] style:UIBarButtonItemStyleDone target:self action:@selector(click)];

        

        //设置当前页面中显示在导航条右边的专用按钮

    //    self.navigationItem.rightBarButtonItem = bbbi;

        

        //3,使用系统自带的图标创建专用按钮

        UIBarButtonItem *cbbi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(click)];

    //    self.navigationItem.rightBarButtonItem = cbbi;

        

        //有一个btn

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];

        [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];

        

        //4,使用自定义的view来创建专用按钮(一般都是用btn)

        UIBarButtonItem *dbbi = [[UIBarButtonItem alloc] initWithCustomView:btn];

    //    self.navigationItem.rightBarButtonItem = dbbi;

        

        //设置右边的一组专用按钮

        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bbbi, cbbi, dbbi, nil];

        

        

    }

     

    - (void)click

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)backClick

    {

        //通过页面找到nc,让nc返回到上一面(效果和原理都跟nc自带的返回按钮一样)

        [self.navigationController popViewControllerAnimated:YES];

    }

     

    #import "ThirdViewController.h"

    #import "ForthViewController.h"

     

    @interface ThirdViewController ()

     

    @end

     

    @implementation ThirdViewController

     

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

        }

        return self;

    }

     

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {

        ForthViewController *fvc = [[ForthViewController alloc] init];

        

        [self.navigationController pushViewController:fvc animated:YES];

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        self.view.backgroundColor = [UIColor yellowColor];

     

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame = CGRectMake(60, 70, 100, 40);

        btn.backgroundColor = [UIColor grayColor];

        [btn setTitle:@"返回" forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    }

     

    - (void)backClick

    {

        //返回到根(沿途所有页面全部销毁)

        [self.navigationController popToRootViewControllerAnimated:YES];

    }

     

    - (void)viewWillAppear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewDidAppear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewWillDisappear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewDidDisappear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

    #import "ForthViewController.h"

     

    @interface ForthViewController ()

     

    @end

     

    @implementation ForthViewController

     

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    {

        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

        if (self) {

            // Custom initialization

            NSLog(@"%s",__func__);

        }

        return self;

    }

     

    - (void)viewWillAppear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewDidAppear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewWillDisappear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewDidDisappear:(BOOL)animated

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)dealloc

    {

        NSLog(@"%s",__func__);

    }

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        // Do any additional setup after loading the view.

        NSLog(@"%s",__func__);

        

        self.view.backgroundColor = [UIColor greenColor];

        

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame = CGRectMake(60, 70, 100, 40);

        btn.backgroundColor = [UIColor grayColor];

        [btn setTitle:@"返回" forState:UIControlStateNormal];

        [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:btn];

    }

     

    - (void)backClick

    {

        //找到当前导航控制器里所有的页面

        NSArray *viewControllerArr = self.navigationController.viewControllers;

        

        //找到固定的页面,用父类指针指向

        UIViewController *orVC = [viewControllerArr objectAtIndex:1];

        

        //让nc回到固定页面(必须是在nc中已经存在的,同时沿途所有页面销毁)

        [self.navigationController popToViewController:orVC animated:YES];

    }

     

    让明天,不后悔今天的所作所为
  • 相关阅读:
    MVC的布局页,视图布局页和分布页的使用
    C#程序的编译过程
    页面跳转到Area区域连接
    c#静态变量和非静态变量的区别
    C#设计模式:适配器模式(Adapter Pattern)
    依赖注入
    打印随机数到字符串中
    printf/scanf格式
    用fread和fwrite实现文件复制操作
    用fseek和ftell获取文件的大小
  • 原文地址:https://www.cnblogs.com/-yun/p/4354546.html
Copyright © 2011-2022 走看看