最近接手項(xiàng)目坏晦,里面關(guān)于導(dǎo)航欄的定義非常亂萝玷,然后自己抽空寫(xiě)了個(gè)demo,里面有個(gè)分類(lèi)昆婿,專門(mén)總結(jié)梳理了一下關(guān)于iOS13.0前后的對(duì)于導(dǎo)航條的處理方式:
1.關(guān)于下劃線的處理:
-(void)setNoLine
{
? ? if (@available(iOS 13.0, *)) {
? ? ? ? self.standardAppearance.shadowColor = [UIColor clearColor];
? ? ? } else {
? ? ? ? ? if (@available(iOS 10.0, *))
? ? ? ? ? {
? ? ? ? ? ? ? [self getLineHidden:YES];
? ? ? ? ? }else
? ? ? ? ? {
? ? ? ? ? ? ? self.shadowImage = [UIImage new];
? ? ? ? ? }
? ? ? }
}
2.關(guān)于背景色的處理:
- (void)setBackColor:(UIColor *)backColor
{
? ? if (@available(iOS 13.0, *))
? ? {
? ? ? ? [self.standardAppearance configureWithOpaqueBackground];
? ? ? ? [self.standardAppearance setBackgroundColor:backColor];
? ? ? ? [self.standardAppearance setBackgroundImage:nil];
? ? }else
? ? {
? ? ? ? [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
? ? ? ? [self setBarTintColor:backColor];
? ? }
}
3.關(guān)于導(dǎo)航條透明的處理:
- (void)setBackClear
{
? ? if (@available(iOS 13.0, *))
? ? {
? ? ? ? [self.standardAppearance configureWithTransparentBackground];
? ? ? ? self.standardAppearance.shadowColor = [UIColor clearColor];
? ? }else
? ? {
? ? ? ? [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
? ? ? ? if (@available(iOS 10.0, *))
? ? ? ? {
? ? ? ? ? ? [self getLineHidden:YES];
? ? ? ? }else
? ? ? ? {
? ? ? ? ? ? self.shadowImage = [UIImage new];
? ? ? ? }
? ? }
}
4.關(guān)于重置導(dǎo)航條為系統(tǒng)的樣式處理:
- (void)resetBack
{
? ? if (@available(iOS 13.0, *))
? ? {
? ? ? ? [self.standardAppearance configureWithDefaultBackground];
? ? ? ? self.standardAppearance.shadowImage = nil;
? ? }else
? ? {
? ? ? ? [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
? ? ? ? if (@available(iOS 10.0, *))
? ? ? ? ? {
? ? ? ? ? ? ? [self getLineHidden:NO];
? ? ? ? ? }else
? ? ? ? ? {
? ? ? ? ? ? ? self.shadowImage = nil;
? ? ? ? ? }
? ? }
}
5.ios10上關(guān)于是否隱藏下劃線的處理:
- (void)getLineHidden:(BOOL)hidden
{
? ? if ([self respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)])
? ? ? ? {
? ? ? ? ? ? NSArray *list=self.subviews;
? ? ? ? ? ? for (id obj in list)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? UIView *view = (UIView*)obj;
? ? ? ? ? ? ? ? for (id obj2 in view.subviews) {
? ? ? ? ? ? ? ? ? ? if ([obj2 isKindOfClass:[UIImageView class]])
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? UIImageView *image =? (UIImageView*)obj2;
? ? ? ? ? ? ? ? ? ? ? ? image.hidden = hidden;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
}
6.關(guān)于設(shè)置導(dǎo)航條標(biāo)題font和color的處理:
- (void)setTitleColor:(UIColor *)titleColor font:(UIFont *)font
{
? ? //文字顏色
? ? ? if (@available(iOS 13.0, *))
? ? ? {
? ? ? ? ? [self.standardAppearance setTitleTextAttributes:@{NSFontAttributeName:font , NSForegroundColorAttributeName:titleColor}];
? ? ? }else
? ? ? {
? ? ? ? ? [self setTitleTextAttributes:@{NSFontAttributeName:font , NSForegroundColorAttributeName:titleColor}];
? ? ? }
}
7.傳入一個(gè)返回按鈕圖片名字球碉,自定義導(dǎo)航條返回按鈕樣式的處理:
- (void)setBackImageWithImageName:(NSString *)imageName
{
? ? UIImage *image = [UIImage imageNamed:imageName];
? ? image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
? ? if (@available(iOS 13.0, *))
? ? {
? ? ? ? [self.standardAppearance setBackIndicatorImage:image transitionMaskImage:image];
? ? }else
? ? {
? ? ? ? self.backIndicatorImage = image;
? ? ? ? self.backIndicatorTransitionMaskImage = image;
? ? }
}
具體demo地址:https://github.com/shineDongDongEr/LDLNavBar-Sxs,給我一顆小??仓蛆,??V摺!