ios之导航渐变---/导航透明
ios之导航渐变---/导航透明。
// cznavtableviewcontroller.h
// 导航栏渐变透明效果
#import
@interface cznavtableviewcontroller : uitableviewcontroller
@end
================
// cznavtableviewcontroller.m
// 导航栏渐变透明效果
//
#import "cznavtableviewcontroller.h"
#import "uinavigationbar+alpha.h"
@interface cznavtableviewcontroller ()
@end
@implementation cznavtableviewcontroller
- (void)viewdidload {
[super viewdidload];
//
// [self.navigationcontroller.navigationbar setbackgroundcolor:[uicolor redcolor]];
//
// [self.navigationcontroller.navigationbar setbartintcolor:[uicolor redcolor]];
// [self.navigationcontroller.navigationbar settintcolor:[uicolor redcolor]];
// [self.navigationcontroller.navigationbar setbackgroundimage:[uiimage imagenamed:@"navbar64"] forbarmetrics:uibarmetricsdefault];
}
-(void)scrollviewdidscroll:(uiscrollview *)scrollview
{
//当前的y值
cgfloat offsety = scrollview.contentoffset.y;
uicolor *color = [uicolor redcolor];
cgfloat alpha = (30 + 64 - offsety)/64;
if (offsety > 30) {
[self.navigationcontroller.navigationbar alphanavigationbarview:[color colorwithalphacomponent:alpha]];
}else
{
[self.navigationcontroller.navigationbar alphanavigationbarview:[color colorwithalphacomponent:1]];
}
}
#pragma mark - table view data source
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview {
#warning incomplete implementation, return the number of sections
return 0;
}
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
#warning incomplete implementation, return the number of rows
return 0;
}
=================
#import
@interface uinavigationbar (alpha)
//动态添加的uiview 添加到uinavigationbar 上
@property (nonatomic , strong)uiview *alphaview;
- (void)alphanavigationbarview:(uicolor *)color;
@end
===========
#import "uinavigationbar+alpha.h"
#import
@implementation uinavigationbar (alpha)
static char alview;
-(uiview *)alphaview
{
return objc_getassociatedobject(self, &alview);
}
-(void)setalphaview:(uiview *)alphaview
{
objc_setassociatedobject(self, &alview, alphaview, objc_association_retain_nonatomic);
}
//通过这个方法去改变颜色和透明度
-(void)alphanavigationbarview:(uicolor *)color
{
if (!self.alphaview) {
//设置一张图片,如果不设置,颜色不纯
[self setbackgroundimage:[uiimage new] forbarmetrics:uibarmetricsdefault];
//创建
self.alphaview = [[uiview alloc]initwithframe:cgrectmake(0, -20,[uiscreen mainscreen].bounds.size.width , 64)];
//添加到navigationbar
[self insertsubview:self.alphaview atindex:0];
}
[self.alphaview setbackgroundcolor:color];
}
@end