iOS开发之高级视图—— UINavigationController(一)简单导航功能实现

        UINavigationController是IOS编程中比较常用的一种容器view controller,很多系统的控件以及很多有名的APP中(如qq,系统相册等)都有用到。
        navigationItem是UIViewController的一个属性,这个属性是为UINavigationController服务的。navigationItem在navigationBar代表一个viewController,具体一点儿来说就是每一个加到navigationController的viewController都会有一个对应的navigationItem。

        可以设置leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title以及prompt等属性。

        PS:从iOS 7开始,苹果引入了一个[UIViewController setEdgesForExtendedLayout:]的新属性,它的默认值为UIRectEdgeAll。

        当我们的容器是UINavigationController的时候,默认的布局将从navigation bar的顶部开始。
那么也就是说我们的组件(例如UITableView)将 可以在NavigationBar 的下面看见。

       解决这个问题有2种快速方法就是

             1: 
              在viewController的- (void)viewDidLoad方法中添加如下代码:
              self.edgesForExtendedLayout = UIRectEdgeNone;
             但是本方式仅对当前的viewController有效


            2:
             将NavigationController的navigationBar的模糊属性改为NO。
             navigation.navigationBar.translucent = NO;



         AppDelegate.m

//
//  AppDelegate.m
//  UINavigationControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    
    //创建ViewController
    ViewController* viewController = [[ViewController alloc] init];

    
    // 创建UINavigationController
    UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
    // 将NavigationController的navigationBar的模糊属性改为NO。
    //    navigationController.navigationBar.translucent = NO;
    
    // 设置导航栏样式
    navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
    // 隐藏navigationBar状态栏,默认NO
    //    [navigationController setNavigationBarHidden:YES];
    
    // 隐藏navigation控制器的Tool Bar 工具栏,默认YES
    [navigationController setToolbarHidden:NO];
    
    // 设置UINavigationController为rootViewController
    self.window.rootViewController = navigationController;
    
    [self.window makeKeyAndVisible];
    
    
    return YES;
}
@end

     ViewController.m

//
//  ViewController.m
//  UINavigationControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "ViewController.h"
#import "HeroViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.navigationItem.title = @"英雄联盟";
    self.navigationItem.prompt = @"你!准备好了吗?";
    
    UILabel* show = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
    show.text = @"展示你的实力吧";
    show.textColor = [UIColor blueColor];
    show.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = show;
    
    /*
     // 自定义文字按钮
     UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithTitle:@"新增" style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
     [addBtn setTag:1];
     
     UIBarButtonItem *delBtn = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
     [delBtn setTag:2];
     */
    /*
     // 自定义图片按钮
     UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bank"] style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
     [addBtn setTag:1];
     
     UIBarButtonItem *delBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"gear"] style:UIBarButtonItemStylePlain target:self action:@selector(onClick:)];
     [delBtn setTag:2];
     */
    
    // 定义系统按钮
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(onClick:)];
    [addBtn setTag:1];
    
    UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onClick:)];
    [searchBtn setTag:2];
    
    self.navigationItem.leftBarButtonItem = addBtn;
    self.navigationItem.rightBarButtonItem = searchBtn;
    
    // 在左边增加多个按钮
    //    self.navigationItem.leftBarButtonItems = @[addBtn,delBtn];
    
    /*****Tool Bar 工具栏*********/
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit  target:self action:@selector(onClick:)];
    [editBtn setTag:3];
    
    UIBarButtonItem *playBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(onClick:)];
    [playBtn setTag:4];
    
    // Tool Bar 工具栏添加按钮
    self.toolbarItems = @[editBtn,playBtn];
    
}

-(void) onClick : (id) sender{
    UIBarButtonItem* button = (UIBarButtonItem*)sender;
    if (button.tag == 1) {
        NSLog(@"----新增----------");
    }
    else if(button.tag == 2) {
        NSLog(@"----跳转----------");
        
        HeroViewController* heroView = [[HeroViewController alloc] init];
        
        
        [self.navigationController pushViewController:heroView animated:YES];
    }
}




@end

    HeroViewController.h

//
//  HeroViewController.h
//  UINavigationControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface HeroViewController : UITableViewController

@end

      HeroViewController.m

//
//  HeroViewController.m
//  UINavigationControllerApp
//
//  Created by Apple on 16/5/26.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "HeroViewController.h"

@interface HeroViewController ()

@end

NSArray* heroList;

@implementation HeroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化数组
    heroList = @[@"李青",@"瑞文",@"瑞兹",@"提莫",@"阿狸"];
    // 设置title
    self.navigationItem.title=@"英雄池";
    
    // 设置edgesForExtendedLayout属无效。但是本方式仅对当前的viewController有效
    //self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    // UITableViewController默认的会在viewWillAppear的时候,清空所有选中cell,我们可以通过设置self.clearsSelectionOnViewWillAppear = NO,来禁用该功能,并在viewDidAppear中调用UIScrollView的flashScrollIndicators方法让滚动条闪动一次,从而提示用户该控件是可以滑动的。
    //     self.clearsSelectionOnViewWillAppear = NO;
    
    // 设置一个edit按钮
    //     self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    // 注册UITableViewCell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [heroList count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 获取UITableViewCell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    // 设置每一列的text
    cell.textLabel.text = heroList[indexPath.row];
    
    return cell;
}


@end

       效果图如下:

    以下按钮除了右上角的搜索按钮有响应,其他按钮只是用来展示设置的方法。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值