1.当第一次用到这个类的时候就会掉用以下方法:
+ (void)initialize {
}
2.push和pop的使用:
1 #pragma mark - 拦截所有push方法 2 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { 3 4 if (self.viewControllers.count > 0) { 5 // 如果navigationController的字控制器个数大于两个就隐藏 6 viewController.hidesBottomBarWhenPushed = YES; 7 8 viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(back)]; 9 } 10 [super pushViewController:viewController animated:YES]; 11 } 12 13 #pragma mark - 拦截所有pop方法 14 - (void)back { 15 [super popViewControllerAnimated:YES]; 16 }
3.实现拖拽表视图的时候隐藏导航栏和标签栏:
1 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { 2 [UIView animateWithDuration:1 animations:^{
//改变标签栏的fram,隐藏 3 self.tabBarController.tabBar.transform = CGAffineTransformMakeTranslation(0, 49); 4 [UIView animateWithDuration:5 animations:^{
//改变导航栏的透明度 5 self.navigationController.navigationBar.alpha = 0; 6 }]; 7 }]; 8 9 } 10 11 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { 12 [UIView animateWithDuration:1 animations:^{ 13 self.tabBarController.tabBar.transform = CGAffineTransformIdentity; 14 self.navigationController.navigationBar.alpha = 1; 15 }]; 16 }
4.实现下拉表视图表头图片的等比放大,停止滑动图片还原大小至滑走:
1 #pragma mark - 重点的地方在这里 滚动时候进行计算 2 -(void)scrollViewDidScroll:(UIScrollView *)scrollView { 3 CGFloat offsetY = scrollView.contentOffset.y; //-264 4 NSLog(@"%f",offsetY); 5 6 //取得偏移的高度 7 CGFloat offsetH = imageBGHeight + offsetY; 8 9 //往下拉 10 if (offsetH < 0) { 11 CGRect frame = self.imageBG.frame; 12 13 //改变图片的高度 14 frame.size.height = imageBGHeight - offsetH; 15 16 //图片的y要往上走 17 frame.origin.y = -imageBGHeight + offsetH; 18 self.imageBG.frame = frame; 19 } 20 21 CGFloat alpha = offsetH / imageBGHeight; 22 23 [self.navigationController.navigationBar setBackgroundImage:[self imageWithColor:[[UIColor cyanColor] colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault]; 24 self.titleLabel.alpha = alpha; 25 } 26 27 #pragma mark - 返回一张纯色图片 28 /** 返回一张纯色图片 */ 29 - (UIImage *)imageWithColor:(UIColor *)color { 30 // 描述矩形 31 CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f); 32 // 开启位图上下文 33 UIGraphicsBeginImageContext(rect.size); 34 // 获取位图上下文 35 CGContextRef context = UIGraphicsGetCurrentContext(); 36 // 使用color演示填充上下文 37 CGContextSetFillColorWithColor(context, [color CGColor]); 38 // 渲染上下文 39 CGContextFillRect(context, rect); 40 // 从上下文中获取图片 41 UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext(); 42 // 结束上下文 43 UIGraphicsEndImageContext(); 44 return theImage; 45 }
5.实现十六进制转颜色的宏定义:
1 #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
6.去掉导航分割线:
1 // 去掉导航分割线 2 [self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]; 3 [self.navigationBar setShadowImage:[[UIImage alloc] init]];