问:UITableView scrollViewDidScroll函数可以更改自定义navigationBar的alpha

时间:2016-07-26 08:09:50

标签: ios uitableview scroll alpha

在我的view controller中,我有一个tableView,我还在view controller的视图中添加了自定义导航栏,我希望淡化{{1}我的自定义navigationBar,但它似乎没用,我打印了自定义navigationBar的alpha,它显示没问题,但我的自定义navigationBar看起来没有变化alpha,这是我的代码:

alpha

顺便说一下,我测试了使用#pragma mark -- scorllView delegate - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if(scrollView == _tableview){ CGFloat currentOffsetY = scrollView.contentOffset.y; // 0 ~ 358 -20 ~ 338 float alpha_of_scroll = (currentOffsetY + 20) / 358.0; NSLog(@"%f", alpha_of_scroll); _narBarView.alpha = 1 - alpha_of_scroll; NSLog(@"%f", _narBarView.alpha); } } ,它与reactive cocoa函数的显示方式相同。

1 个答案:

答案 0 :(得分:0)

试试这个:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, CGRectGetWidth(self.navigationController.navigationBar.bounds), CGRectGetHeight(self.navigationController.navigationBar.bounds) + 20)];
    overlay.userInteractionEnabled = NO;
    overlay.backgroundColor = [UIColor orangeColor];
    [self.navigationController.navigationBar insertSubview:overlay atIndex:0];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}


#pragma mark -- scorllView delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if(scrollView == self.myTableView){
        CGFloat offsetY = scrollView.contentOffset.y;
        NSLog(@"%f", offsetY);
        if (offsetY > -64) {
            if (offsetY >= 0) {
                [self setNavigationBarAlpha:0];
            } else {
                [self setNavigationBarAlpha:(-offsetY / 64)];
            }
        } else {
            [self setNavigationBarAlpha:1];
        }
    }
}

- (void)setNavigationBarAlpha:(CGFloat)alpha {
    [[self.navigationController.navigationBar subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
        obj.alpha = alpha;
    }];
}