??向下滑動Tableview顯示導(dǎo)航??

困難是成功的階梯,爬著很累,但不能輕言放棄。
把經(jīng)驗藏在肚子里的人,不會成功

首先在AppDelegate.m中

#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//創(chuàng)建導(dǎo)航    
    ViewController *vv=[[ViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vv];
    self.window.rootViewController=nav;
    
    return YES;
}

之后再ViewController.m中

#import "ViewController.h"
#define IPHONE_WIDTH  [UIScreen mainScreen].bounds.size.width
#define IPHONE_HEIGHT [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * table;
    UIView * _myNavigationView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title =@"向下滑動表格顯示導(dǎo)航";
    self.view.backgroundColor =[UIColor blackColor];
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    table.backgroundColor =[UIColor whiteColor];
    table.delegate =self;
    table.dataSource =self;
    [self.view addSubview:table];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    _myNavigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 64)];
    _myNavigationView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:_myNavigationView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID =@"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;
}
- ( UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
{
    return nil;
}
- ( UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;
{
    return nil;
}

#pragma mark - scroll Delegate
//重要的東西都在這個方法里
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int contentOffSetY = scrollView.contentOffset.y;
    
    if (contentOffSetY< 64)
    { _myNavigationView.alpha= scrollView.contentOffset.y/64; }
    else
    {
        _myNavigationView.alpha=1;
    }
    if (contentOffSetY<0)
    {
//        突然滑動的并且幅度很大的請款下 會在原本的導(dǎo)航view上面64的高度   
        //        CGRect rect =_myNavigationView.frame; rect.origin.y =0;
        //        rect.size.height=-contentOffSetY+64;
        //        _myNavigationView.frame =rect;
    }   
}
@end
baige 1.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容