0:首先還是通過純的代碼來實現
0:刪除3個檔案ViewController.h,ViewController.m,Main.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