实在不知道怎么给这篇文章起一个比较好的名字,我们还是先看下效果吧
效果图
下面看实现,这边以设置tableView的headerView为例来讲解
老样子,直接上代码
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1d9421}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #6122ae}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s3 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s4 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s5 {font-variant-ligatures: no-common-ligatures; color: #294c50}span.s6 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s7 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s8 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s9 {font-variant-ligatures: no-common-ligatures; color: #0435ff}span.s10 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s11 {font-variant-ligatures: no-common-ligatures; color: #c91b13}
- (void)setTableViewHeaderView {
//将navigationBar的背景设为透明
[self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
//去掉navigationBar下面的那条分割线
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
CGFloat headerViewHeight = 200;
UIView* headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , headerViewHeight)];
UIImageView* imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"123.jpg"]];
//这个-64非常关键
imgView.frame = CGRectMake(0, -64, SCREEN_WIDTH, headerViewHeight + 64);
[headerView addSubview:imgView];
self.tableView.tableHeaderView = headerView;
}
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #822e0e}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1d9421}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s4 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s5 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}span.s6 {font-variant-ligatures: no-common-ligatures; color: #0435ff}span.s7 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s8 {font-variant-ligatures: no-common-ligatures; color: #78492a}span.s9 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s10 {font-variant-ligatures: no-common-ligatures; color: #294c50}
#pragma mark - UIScrollViewDelegate
//实现UIScrollView的代理方法以实现navigationBar滚动时颜色变化
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UIColor * color = [UIColor colorWithRed:0 green:175/255.0 blue:240/255.0 alpha:1];
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY > NAVBAR_CHANGE_POINT) {
CGFloat alpha = MIN(1, 1 - ((NAVBAR_CHANGE_POINT + 64 - offsetY) / 64));
[self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
} else {
[self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
}
}
** 原理 **
设置tableView的headerView
当我们给定一个view给tableView的headerView时候,这个view的y值会自动被放置到navigationBar的下面
但是,我们再在这个view上添加一个view,如上面代码中 [headerView addSubview:imgView]
然后将imgView.frame = CGRectMake(0, -64, SCREEN_WIDTH, headerViewHeight + 64);
这个frame的y值设为-64,刚好超出navigationBar和statusBar
而navigationBar我们又设成了透明的,statusBar又在view的上方,这样就巧妙的实现了例子gif图中的效果
下面粘一个github上看到的一个navigationBar的分类,写的很漂亮
** .h 文件 **
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1d9421}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c91b13}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c32275}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font: 14.0px 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #822e0e}span.s4 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s5 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}
/**
* 快速方便设置navigation bar
*/
#import <UIKit/UIKit.h>
@interface UINavigationBar (Awesome)
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor;
- (void)lt_setElementsAlpha:(CGFloat)alpha;
- (void)lt_setTranslationY:(CGFloat)translationY;
- (void)lt_reset;
@end
** .m 文件 **
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c91b13}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; min-height: 16.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #3d1d81}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #703daa}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #1d9421}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #c32275}span.s1 {font-variant-ligatures: no-common-ligatures; color: #822e0e}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font-variant-ligatures: no-common-ligatures; color: #c32275}span.s4 {font-variant-ligatures: no-common-ligatures; color: #6122ae}span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s6 {font-variant-ligatures: no-common-ligatures; color: #539aa4}span.s7 {font-variant-ligatures: no-common-ligatures; color: #294c50}span.s8 {font-variant-ligatures: no-common-ligatures; color: #0435ff}span.s9 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s10 {font-variant-ligatures: no-common-ligatures; color: #c91b13}span.s11 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
#import "UINavigationBar+Awesome.h"
#import <objc/runtime.h>
@implementation UINavigationBar (Awesome)
static char overlayKey;
- (UIView *)overlay
{
return objc_getAssociatedObject(self, &overlayKey);
}
- (void)setOverlay:(UIView *)overlay
{
objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)lt_setBackgroundColor:(UIColor *)backgroundColor
{
if (!self.overlay) {
[self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, CGRectGetHeight(self.bounds) + 20)];
self.overlay.userInteractionEnabled = NO;
self.overlay.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self insertSubview:self.overlay atIndex:0];
}
self.overlay.backgroundColor = backgroundColor;
}
- (void)lt_setTranslationY:(CGFloat)translationY
{
self.transform = CGAffineTransformMakeTranslation(0, translationY);
}
- (void)lt_setElementsAlpha:(CGFloat)alpha
{
[[self valueForKey:@"_leftViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
view.alpha = alpha;
}];
[[self valueForKey:@"_rightViews"] enumerateObjectsUsingBlock:^(UIView *view, NSUInteger i, BOOL *stop) {
view.alpha = alpha;
}];
UIView *titleView = [self valueForKey:@"_titleView"];
titleView.alpha = alpha;
// when viewController first load, the titleView maybe nil
[[self subviews] enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:NSClassFromString(@"UINavigationItemView")]) {
obj.alpha = alpha;
*stop = YES;
}
}];
}
- (void)lt_reset
{
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.overlay removeFromSuperview];
self.overlay = nil;
}
@end
希望会对大家有帮助O(∩_∩)O!!!
网友评论