導(dǎo)航欄漸隱透明

有很多項目中用了導(dǎo)航欄項目漸隱的效果,在滑動頁面的時候顯示導(dǎo)航欄######
  • 系統(tǒng)的導(dǎo)航欄是不能滿足這個需求的(目前筆者不知道系統(tǒng)的導(dǎo)航欄可以完成這個需求)

  • 我是通過自定義的View來代替系統(tǒng)的導(dǎo)航欄去完成這個需求

  • 廢話不多說現(xiàn)在開始擼代碼

1. 如果你給當(dāng)前的控制器加了導(dǎo)航欄之后你需要把系統(tǒng)的給隱藏掉
- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    self.navigationController.delegate = self;
}

2. 懶加載創(chuàng)建一個tableView
#pragma mark -- /*init*/
- (UITableView *)tableView {

        if (!_tableView) {
    
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}
- (UIView *)navigationView {

    if (!_navigationView) {
    
        _navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
        _navigationView.backgroundColor = [UIColor whiteColor];
        _navigationView.alpha = 0.0;
        [_navigationView addSubview:self.titleLabel];
        [_navigationView addSubview:self.backButton];
    }
    return _navigationView;
}
- (UIView *)headerView {

    if (!_headerView) {
    
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
        [_headerView addSubview:self.headerImageView];
    }
    return _headerView;
}
- (UIImageView *)headerImageView {
    
    if (!_headerImageView) {
    
        _headerImageView = [[UIImageView alloc] initWithFrame:self.headerView.bounds];
        _headerImageView.image = [UIImage imageNamed:@"1234"];
    }
    return _headerImageView;
  }
- (UILabel *)titleLabel {

    if (!_titleLabel) {
    
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 64)];
        _titleLabel.center = self.navigationView.center;
        _titleLabel.text = @"西天取經(jīng)";
        _titleLabel.alpha = 0.0;
    }
        return _titleLabel;
}
- (UIButton *)backButton {

    if (!_backButton) {
    
        _backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 64)];
        [_backButton setImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
        _backButton.alpha = 0.0;
    }
    return _backButton;
}
3. 把你的tableView和自定義的View以及其他控件都add進去

- (void)viewDidLoad {

    [super viewDidLoad];

    [self.view addSubview:self.tableView];

    [self.view addSubview:self.navigationView];

}
4. 接下來就是tableView的代理和數(shù)據(jù)源方法
#pragma mark -- /*tableViewData&Delegate*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

     return 15;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) {
    
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    return cell;
}

這個地方是最重要的地方通過監(jiān)聽滾動的位置去判斷是否顯示導(dǎo)航欄通過yOffset的大小去實時改變導(dǎo)航欄的alpha值從而實現(xiàn)漸隱效果

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat yOffset = scrollView.contentOffset.y;

    if (yOffset > 0) {

        self.navigationView.alpha = yOffset / 64;
        self.titleLabel.alpha = yOffset / 64;
        self.backButton.alpha = yOffset / 64;
    
    }else if (yOffset <= 0) {

        self.navigationView.alpha = 0.0;
        self.titleLabel.alpha = 0.0;
        self.backButton.alpha = 0.0;
    }

}

最后附上定義的幾個屬性

@property(nonatomic,strong)UITableView *tableView;/**< 界面布局*/

@property(nonatomic,strong)UIView *navigationView;/**< 漸變的nav*/
@property(nonatomic,strong)UIView *headerView;/**< 頭*/
@property(nonatomic,strong)UIImageView *headerImageView;/**< 圖片*/

@property(nonatomic,strong)UILabel *titleLabel;/**< 標題*/

@property(nonatomic,strong)UIButton *backButton;/**< 返回按鈕*/

不要忘記遵循tableView的代理和數(shù)據(jù)源哦~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,725評論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,026評論 4 61
  • 下午兩點午睡前定了三點的鐘 結(jié)果還是六點半才醒過來 做了四個多鐘頭的夢 夢里有初戀男友 還有在深藍色的海洋中坐快艇...
    不吃菠蘿會死星人閱讀 115評論 0 0
  • 工信部規(guī)劃給移動的頻段 A頻段 :2010M~2025M D頻段 :2570M~2620M F頻段 :1880M~...
    啊啊啊ying啊閱讀 404評論 1 0
  • 圖片發(fā)自簡書App 來這座城市的第六天,在剛剛低頭洗菜的時候突然羨慕起一個人--阿飛。 他大概是一個貪玩的孩子,每...
    阿乙同學(xué)閱讀 421評論 0 0

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