效果图
实现步骤
1、设置导航栏视图控制器中的navigationBar
背景图标
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
2、设置导航栏视图控制器中的navigationBar
子视图的背景颜色
barView = self.navigationController.navigationBar.subviews.firstObject;
barView.backgroundColor = [UIColor redColor];
3、实现代理方法- (void)scrollViewDidScroll:(UIScrollView *)scrollView
并在该方法中根据滑动偏移量计算背景色的透明度
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offY = scrollView.contentOffset.y;
NSLog(@"offY: %f", offY);
// 1
if (offY >= 300.0) {
barAlpha = 1.0;
} else if (offY >= 0) {
barAlpha = offY / 300.0;
} else {
barAlpha = 0.0;
}
barView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:barAlpha];
}
代码示例
@interface NavViewController () <UITableViewDelegate, UITableViewDataSource>
{
UITableView *tableview;
CGFloat barAlpha;
UIView *barView;
}
@end
@implementation NavViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"导航栏渐变";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"bottom" style:UIBarButtonItemStyleDone target:self action:@selector(bottomClick)];
// 隐藏分割线
self.navigationController.navigationBar.shadowImage = [UIImage new];
// 设置导航栏背景图标
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
// 设置导航栏背景透明度
barAlpha = 0.0;
barView = self.navigationController.navigationBar.subviews.firstObject;
barView.backgroundColor = [UIColor redColor];
//
tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
[self.view addSubview:tableview];
tableview.autoresizingMask = UIViewAutoresizingFlexibleHeight;
tableview.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tableBground"]];
tableview.delegate = self;
tableview.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
NSLog(@"dealloc: %@", self);
}
- (void)bottomClick
{
[tableview setContentOffset:CGPointMake(0.0, (tableview.contentSize.height - tableview.bounds.size.height)) animated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
cell.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
}
cell.textLabel.text = [NSString stringWithFormat:@"row: %@", @(indexPath.row + 1)];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat offY = scrollView.contentOffset.y;
if (offY >= 300.0) {
barAlpha = 1.0;
} else if (offY >= 0) {
barAlpha = offY / 300.0;
} else {
barAlpha = 0.0;
}
barView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:barAlpha];
}
@end