iOS 第10课 ,导航栏动态变化效果

本文介绍如何在iOS应用中使用纯代码实现一个带有渐变效果的自定义导航栏。主要内容包括删除Storyboard中的默认视图,手动创建并配置MainViewController,以及在App Delegate中进行必要的设置。




0:首先还是通过纯的代码来实现

0:删除3个文件ViewController.hViewController.mMain.storyboard

1:修改点击左边的蓝色按钮,然后选择general-》developer info-》main interface ,将这个main interface 晴空

2:然后再创建一个MainUIViewController ,它继承自UIViewController


1:更新appdelegate.m文件

//
//  AppDelegate.m
//  TenNavigationViewTableView
//
//  Created by 千雅爸爸 on 16/10/16.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.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];
    MainViewController *mainViewController=[[MainViewController alloc]init];
    //如果这里不填写的是UINavigationController 那么导航栏是不显示的
    //注意这里一定要填写的是UINavigationController
    UINavigationController *uiNavigationController = [[UINavigationController alloc]initWithRootViewController:mainViewController];
    [self.window setRootViewController:uiNavigationController];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end



2:MainViewController.m

//
//  MainViewController.m
//  TenNavigationViewTableView
//
//  Created by 千雅爸爸 on 16/10/16.
//  Copyright © 2016年 kodulf. All rights reserved.
//

#import "MainViewController.h"
//TODO 切记切记,这里一定要设置实现协议,不然scrollViewDidScroll就不能够找到
@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;//TableView其实就是android 中的listview
@property (nonatomic,strong) UINavigationBar *navigationBar;

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor greenColor]];
    //隐藏系统自带的导航条,因为我们要实现渐变的效果,导航栏是自定义的
    [self.navigationController setNavigationBarHidden:YES];
    //让导航栏不影响tableview的视图,这样添加的起始坐标,
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    //这样添加的tableview就不会做自动挡饿调整了
    [self intiWithNavigationBar];
    
    [self initTableView];
    
    // Do any additional setup after loading the view.
}

-(void) initTableView{
    [self setTableView:[[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) style:UITableViewStylePlain]];//CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)获取空间的长度,有点像android 里面的获取空间长度的方法
    [self.tableView setDataSource:self];
    [self.tableView setDelegate:self];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.tableView setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];//有点像android 里面的padding上坐下右;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.view addSubview:self.tableView];
    
    

}

-(void)intiWithNavigationBar{
    [self setNavigationBar:[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64)]];
    [self.navigationBar setBackgroundColor:[UIColor yellowColor]];//
    //将不透明的效果取消掉
    [self.navigationBar setTranslucent:NO];
    [self.navigationController.view addSubview:self.navigationBar];
    
    //如果背景层不移除的话,会有问题
    for (UIView *subView in self.navigationBar.subviews) {
        if([subView isKindOfClass:[NSClassFromString(@"_UINavigationBarBackground") class]]){
            [subView removeFromSuperview];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view delegate -
//这里相当于java中的实现了接口后的方法,
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    UIColor *color = self.navigationBar.backgroundColor;
    CGFloat offsetY = scrollView.contentOffset.y;//往上为正
    if(offsetY>0){
        CGFloat alpha  = (600-offsetY)/600;
        self.navigationBar.backgroundColor =[color colorWithAlphaComponent:alpha];
    }else{
        self.navigationBar.backgroundColor =[color colorWithAlphaComponent:1];
    }
}

#pragma mark - Table view data source -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];//会从重用队列里面去取
    [cell.textLabel setText:[NSString stringWithFormat:@"Row-%zd",indexPath.row]];
    [cell setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:1.0/(arc4random()%5)] ];//产生随机数的方法,arc4random(),和java略有不同
    
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值