關(guān)于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?

每個屬性又有相關(guān)屬性若干:

/// 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?)

與我們平時設(shè)置導航欄屬性相同萨赁,只不過做一些區(qū)分锅风,在iOS13中使用這些來設(shè)置就好了卷哩。

但是,當你以為就這么簡單的話珊擂,那就想多了,運行程序時你會發(fā)現(xiàn)吼驶,并沒有達到想要的效果翼抠,比如,我想大標題整體呈現(xiàn)一個顏色扼鞋,但是你如果只設(shè)置self.navigationController?.navigationBar.barTintColor = UIColor.red申鱼,只會在你上滑的時候,一部分會顯示為紅色云头,而下面的部分沒有任何變化捐友,尷尬!而新的屬性又沒有提供可以設(shè)置背景色的東西盘寡,當然這是你建立在你對新東西不熟悉的情況下楚殿,繼續(xù)往下看。

既然這個類(UINavigationBarAppearance)中沒有,那么我們往上找脆粥,發(fā)現(xiàn)它繼承于(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砌溺,當然還有一些其他屬性,這里就不介紹了变隔。
那么通過以下設(shè)置就達到想要的效果了??

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 
}

需要注意的是规伐,設(shè)置了standardAppearance.backgroundColor,那么navigationBar.barTintColor就會失效匣缘!好了猖闪,問題就到這里!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末肌厨,一起剝皮案震驚了整個濱河市培慌,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌柑爸,老刑警劉巖吵护,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異表鳍,居然都是意外死亡馅而,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門譬圣,熙熙樓的掌柜王于貴愁眉苦臉地迎上來瓮恭,“玉大人,你說我怎么就攤上這事厘熟⊥捅模” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵盯漂,是天一觀的道長颇玷。 經(jīng)常有香客問我,道長就缆,這世上最難降的妖魔是什么帖渠? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮竭宰,結(jié)果婚禮上空郊,老公的妹妹穿的比我還像新娘。我一直安慰自己切揭,他們只是感情好狞甚,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著廓旬,像睡著了一般哼审。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天涩盾,我揣著相機與錄音十气,去河邊找鬼。 笑死春霍,一個胖子當著我的面吹牛砸西,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播址儒,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼芹枷,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了莲趣?” 一聲冷哼從身側(cè)響起鸳慈,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎喧伞,沒想到半個月后蝶涩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡絮识,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了嗽上。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片次舌。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖兽愤,靈堂內(nèi)的尸體忽然破棺而出彼念,到底是詐尸還是另有隱情,我是刑警寧澤浅萧,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布逐沙,位于F島的核電站,受9級特大地震影響洼畅,放射性物質(zhì)發(fā)生泄漏吩案。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一帝簇、第九天 我趴在偏房一處隱蔽的房頂上張望徘郭。 院中可真熱鬧,春花似錦丧肴、人聲如沸残揉。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽抱环。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間镇草,已是汗流浹背眶痰。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留陶夜,地道東北人凛驮。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像条辟,于是被迫代替她去往敵國和親黔夭。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內(nèi)容