关于iOS13导航栏的坑

iOS13 navigationBar新增了以下是三个属性

/*
Fallback Behavior:
1) Appearance objects are used in whole – that is, all values will be sourced entirely from an instance of UINavigationBarAppearance defined by one of these named properties (standardAppearance, compactAppearance, scrollEdgeAppearance) on either UINavigationBar (self) or UINavigationItem (self.topItem).
2) The navigation bar will always attempt to use the most relevant appearance instances first, before falling back to less relevant ones. The fallback logic is:
AtScrollEdge: self.topItem.scrollEdgeAppearance => self.scrollEdgeAppearance => self.topItem.standardAppearance => self.standardAppearance
CompactSize: self.topItem.compactAppearance => self.compactAppearance => self.topItem.standardAppearance => self.standardAppearance
NormalSize: self.topItem.standardAppearance => self.standardAppearance
*/

/// Describes the appearance attributes for the navigation bar to use when it is displayed with its standard height.
@available(iOS 13.0, *)
@NSCopying open var standardAppearance: UINavigationBarAppearance

/// Describes the appearance attributes for the navigation bar to use when it is displayed with its compact height. If not set, the standardAppearance will be used instead.
@available(iOS 13.0, *)
@NSCopying open var compactAppearance: UINavigationBarAppearance?

/// Describes the appearance attributes for the navigation bar to use when an associated UIScrollView has reached the edge abutting the bar (the top edge for the navigation bar). If not set, a modified standardAppearance will be used instead.
@available(iOS 13.0, *)
@NSCopying open var scrollEdgeAppearance: UINavigationBarAppearance?

每个属性又有相关属性若干:

/// Inline Title text attributes. If the font or color are unspecified, appropriate defaults are supplied.
open var titleTextAttributes: [NSAttributedString.Key : Any]

/// An additional adjustment to the inline title's position.
open var titlePositionAdjustment: UIOffset


/// Large Title text attributes. If the font or color are unspecified, appropriate defaults are supplied.
open var largeTitleTextAttributes: [NSAttributedString.Key : Any]


/// The appearance for plain-style bar button items
@NSCopying open var buttonAppearance: UIBarButtonItemAppearance


/// The appearance for done-style bar button items
@NSCopying open var doneButtonAppearance: UIBarButtonItemAppearance


/// The appearance for back buttons. Defaults are drawn from buttonAppearance when appropriate.
@NSCopying open var backButtonAppearance: UIBarButtonItemAppearance


/// The image shown on the leading edge of the back button.
open var backIndicatorImage: UIImage { get }

/// This image is used to mask content flowing underneath the backIndicatorImage during push & pop transitions
open var backIndicatorTransitionMaskImage: UIImage { get }

/// Set the backIndicatorImage & backIndicatorTransitionMaskImage images. If either image is nil, then both images will be reset to their default.
open func setBackIndicatorImage(_ backIndicatorImage: UIImage?, transitionMaskImage backIndicatorTransitionMaskImage: UIImage?)

与我们平时设置导航栏属性相同,只不过做一些区分,在iOS13中使用这些来设置就好了。

但是,当你以为就这么简单的话,那就想多了,运行程序时你会发现,并没有达到想要的效果,比如,我想大标题整体呈现一个颜色,但是你如果只设置self.navigationController?.navigationBar.barTintColor = UIColor.red,只会在你上滑的时候,一部分会显示为红色,而下面的部分没有任何变化,尴尬!而新的属性又没有提供可以设置背景色的东西,当然这是你建立在你对新东西不熟悉的情况下,继续往下看。

既然这个类(UINavigationBarAppearance)中没有,那么我们往上找,发现它继承于(UIBarAppearance)

/// Constructs a new bar appearance, configured with default values and targeting the device idiom.
public convenience init()


/// Constructs a new bar appearance, targetting the passed-in idiom as a hint. Not all platforms support all available idioms. See the idiom property to determine the resolved idiom.
public init(idiom: UIUserInterfaceIdiom)


/// The idiom that this appearance object targets.
open var idiom: UIUserInterfaceIdiom { get }


/// Constructs a new bar appearance, copying all relevant properties from the given appearance object. This initializer is useful for migrating configuration between UIBarAppearance subclasses. For example, you can initialize a UINavigationBarAppearance with a UIToolbarAppearance instance, and shared attributes will be identical between the two.
public init(barAppearance: UIBarAppearance)


public init(coder: NSCoder)


open func copy() -> Self


/// Reset background and shadow properties to their defaults.
open func configureWithDefaultBackground()


/// Reset background and shadow properties to display theme-appropriate opaque colors.
open func configureWithOpaqueBackground()


/// Reset background and shadow properties to be transparent.
open func configureWithTransparentBackground()


/// A specific blur effect to use for the bar background. This effect is composited first when constructing the bar's background.
@NSCopying open var backgroundEffect: UIBlurEffect?

/// A color to use for the bar background. This color is composited over backgroundEffects.
@NSCopying open var backgroundColor: UIColor?

/// An image to use for the bar background. This image is composited over the backgroundColor, and resized per the backgroundImageContentMode.
open var backgroundImage: UIImage?

/// The content mode to use when rendering the backgroundImage. Defaults to UIViewContentModeScaleToFill. UIViewContentModeRedraw will be reinterpreted as UIViewContentModeScaleToFill.
open var backgroundImageContentMode: UIView.ContentMode


/// A color to use for the shadow. Its specific behavior depends on the value of shadowImage. If shadowImage is nil, then the shadowColor is used to color the bar's default shadow; a nil or clearColor shadowColor will result in no shadow. If shadowImage is a template image, then the shadowColor is used to tint the image; a nil or clearColor shadowColor will also result in no shadow. If the shadowImage is not a template image, then it will be rendered regardless of the value of shadowColor.
@NSCopying open var shadowColor: UIColor?

/// Use an image for the shadow. See shadowColor for how they interact.
open var shadowImage: UIImage?

而UIBarAppearance类中则有我们需要的属性backgroundColor,当然还有一些其他属性,这里就不介绍了。
那么通过以下设置就达到想要的效果了😊

if #available(iOS 13.0, *) {
     self.navigationController?.navigationBar.standardAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor:  UIColor.rgbColor(rgbValue: 0x000000), NSAttributedString.Key.font: UIFont.systemFont(ofSize: 34)]
     self.navigationController?.navigationBar.standardAppearance.backgroundColor = UIColor.yellow
} else {
     self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor:  UIColor.rgbColor(rgbValue: 0x000000), NSAttributedString.Key.font: UIFont.systemFont(ofSize: 34)]
     self.navigationController?.navigationBar.barTintColor = UIColor.red 
}

需要注意的是,设置了standardAppearance.backgroundColor,那么navigationBar.barTintColor就会失效!好了,问题就到这里!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容