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就會失效匣缘!好了猖闪,問題就到這里!