系统导航栏远远不能满足实际开发需要,并且系统导航栏的控制也非常不方便,一般项目中都会采用自定义导航栏的做法,简而言之就是隐藏系统的,使用自己定制的一个view。由于本人项目中导航栏左右可以为文字,图片,或者带下划线的文字,字体颜色也不尽相同,此外,标题透明度,导航栏背景透明度动态改变等,所以我就自己封装了一个导航栏定制工具,将UI布局与导航栏的item属性定制分开,类似于系统的导航栏定制,可以动态增加item个数。
1.左右item
的定制
typedef NS_ENUM(NSUInteger, LWNavigationBarItemType) {
LWNavigationBarItemOnlyText,//只有文字
LWNavigationBarItemOnlyImage,//只有图片
LWNavigationBarItemTextLine,//带下划线的文字
LWNavigationBarItemMulti//图文混合,只支持左文字右图片,不考虑超长文字的情况
};
@interface LWNavigationBarItem : UIControl
- (instancetype)initWithItemType:(LWNavigationBarItemType)itemType;
- (void)setItemTitle:(NSString *)itemTitle;
- (void)setItemImage:(UIImage *)itemImage;
- (void)setItemTitleColor:(UIColor *)itemTitleColor;
- (void)setItemTitleFont:(UIFont *)itemTitleFont;
- (void)setItemImageSize:(CGSize)itemImageSize;
- (void)setItemConentInsets:(UIEdgeInsets)itemContentInsets;
- (void)showItemLine:(BOOL)show;
- (void)setItemLineColor:(UIColor *)itemLineColor;
- (CGSize)itemSize;
@end
2.导航栏生成:
利用生成的item构建导航栏,和系统的分层类似
- (void)addItemToLeft:(LWNavigationBarItem *)item;
- (void)addItemToRight:(LWNavigationBarItem *)item;
- (void)addItemToTitle:(LWNavigationBarItem *)item;
- (void)reloadItems;
此外可以对导航栏的变化进行操作
1.改变透明度:
- (void)lw_updateNavBarWithAlpha:(CGFloat)alpha;
2.改变底部细线的透明度:
- (void)lw_updateLineAlpha:(CGFloat)alpha;
3.改变标题以及item的隐藏状态:
- (void)lw_updateTitleItemAlpha:(CGFloat)alpha;
- (void)lw_updateLeftItemAlpha:(CGFloat)alpha atIndex:(int)index;
4.更新item
- (void)lw_updateLeftItem:(LWNavigationBarItem *)item atIndex:(int)index;
- (void)lw_updateTitleItem:(LWNavigationBarItem *)item;
- (void)lw_updateRightItem:(LWNavigationBarItem *)item atIndex:(int)index;
5.增加左边或者右边的item
- (void)lw_addNewItemToLeft:(LWNavigationBarItem *)item;
- (void)lw_addNewItemToRight:(LWNavigationBarItem *)item;
上述本人写的一个小库LWNavigationBar中的实现部分,具体引入到项目中,可以进行二次封装:
此外,LWNavigationBar
中还提供了动态隐藏左侧和右侧文字或者按钮的方法,具体的可以去源码中查看
截取几种情况下的图片如下:
动态新增左边Item:
下面截取其中一个使用方式的二次封装,目前处理的点击事件是在控制器中引入一下两个方法(也可以优化为block方式,这里不再举例):
/*左右皆为图片,中间标题为文字,图片文字皆可为空
*/
+ (LWNavigationBar *)yg_createNavigationBarWithBarStyle:(YGNavigationBarStyle)style
target:(UIViewController *)target
leftImage:(UIImage * _Nullable)leftImage
titleText:(NSString * _Nullable )titleText
rightImage:(UIImage * _Nullable )rightImage
barHeight:(CGFloat)barHeight {
LWNavigationBarItem *leftItem = [[LWNavigationBarItem alloc] initWithItemType:LWNavigationBarItemOnlyImage];
[leftItem setItemImage:leftImage];
[leftItem setItemImageSize:CGSizeMake(20 * kXX, 20 * kXX)];
if ([target respondsToSelector:@selector(p_didClickLeftItemAction)]) {
[leftItem addTarget:target action:@selector(p_didClickLeftItemAction) forControlEvents:UIControlEventTouchUpInside];
}
LWNavigationBarItem *rightItem = [[LWNavigationBarItem alloc] initWithItemType:LWNavigationBarItemOnlyImage];
[rightItem setItemImage:rightImage];
[rightItem setItemImageSize:CGSizeMake(20 * kXX, 20 * kXX)];
if ([target respondsToSelector:@selector(p_didClickRightItemAction)]) {
[rightItem addTarget:target action:@selector(p_didClickRightItemAction) forControlEvents:UIControlEventTouchUpInside];
}
LWNavigationBarItem *titleItem = [[LWNavigationBarItem alloc] initWithItemType:LWNavigationBarItemOnlyText];
[titleItem setItemTitleColor:(style == YGNavigationBarStyleDefault ? [UIColor convertHexValueToColor:@"#2f2f2f"] : [UIColor whiteColor])];
[titleItem setItemTitle:titleText];
[titleItem setItemTitleFont:[UIFont fifteenOfSystem]];
LWNavigationBar *bar = [[LWNavigationBar alloc] init];
bar.backgroundColor = (style == YGNavigationBarStyleDefault ? [UIColor whiteColor] : [UIColor createNavigationColor]);
[bar setBarContentInset:15 * kXX];
[bar addItemToTitle:titleItem];
[bar addItemToRight:rightItem];
[bar addItemToLeft:leftItem];
[bar setItemLineViewColor:YGViews_RGBA(182, 182, 182, 1.0)];
bar.frame = CGRectMake(0, 0, kScreenW, barHeight);
[bar reloadItems];
[target.view addSubview:bar];
return bar;
}