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

来源:互联网 发布:中元节生日 知乎 编辑:程序博客网 时间:2024/06/06 16:34




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




0 0
原创粉丝点击