2、导航栏渐变透明面试
思路:给navigationBar一张空的图片,设置frame为0,在navigationBar上自定义view,在运行时添加view,滚动时修改咱们自定义的UIView的alpha就能够实现导航栏渐变透明。app
导航栏的特色:spa
1)导航栏默认有几层 ——>4层orm
2)设置setBarTintColor 导航栏有 6层继承
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];图片
3)设置图片 导航栏有 2层ci
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];get
具体代码it
1)>>建立分类 继承自UINavigationBario
注意:分类能够添加属性(不会生成带下划线的成员变量,没有setter和getter方法),添加属性时,有的时候是不须要建立属性的,能够改变系统自身内部的属性。或者是本身经过运行时建立的。
面试》分类和继承的区别:二者均可以扩展方法和扩展属性。但分类
没有继承关系,耦合度低。分类无继承关系,只是扩充方法。
2) #import <objc/runtime.h>
@implementation UINavigationBar (NavAlpha)
static char alView;
-(void)setAlphaView:(UIView *)alphaView
{
objc_setAssociatedObject(self, &alView, alphaView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIView *)alphaView
{
return objc_getAssociatedObject(self, &alView);
}
3) -(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
UIColor *color = [UIColor redColor];
//渐变透明
//当前滚动的Y值
CGFloat scrollY = scrollView.contentOffset.y;
CGFloat alphaScale = ( 30 + 64 - scrollY)/64;
if (scrollY > 30) {
//渐变
// NSLog(@"%f",alphaScale);
// self.navigationController.navigationBar.alpha = alphaScale;
[self.navigationController.navigationBar setalphaNavigationWithColor:[color colorWithAlphaComponent:alphaScale]];
}else
{
// self.navigationController.navigationBar.alpha = 1;
[self.navigationController.navigationBar setalphaNavigationWithColor:[color colorWithAlphaComponent:1]];
}
}
4) //设置渐变
-(void)setalphaNavigationWithColor:(UIColor *)color
{
if (!self.alphaView) {
//设置背景图片
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//建立自定义透明的view
self.alphaView = [[UIView alloc]initWithFrame:CGRectMake(0,-20, [UIScreen mainScreen].bounds.size.width, 64)];
//插入添加
[self insertSubview:self.alphaView atIndex:0];
}
//设置透明的颜色
[self.alphaView setBackgroundColor:color];
}