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就会失效!好了,问题就到这里!