3

你如何使用这个新对象来自定义 iOS 13 中的导航栏?我在 Objective-C 中尝试了以下代码,但它无法正常工作。它仅在视图控制器被推送或弹出到导航堆栈时显示我的标题文本属性。

UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.titleTextAttributes = @{NSFontAttributeName: font};

这是此对象的文档。 https://developer.apple.com/documentation/uikit/uinavigationbarappearance?language=objc

4

3 回答 3

12

仅仅创建一个UINavigationBarAppearance. 您必须在UINavigationBar实例(或其外观代理)上实际设置它。

// Setup the nav bar appearance
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
appearance.titleTextAttributes = @{NSFontAttributeName: font};

// Apply it to a specific nav bar
someNavBarInstance.standardAppearance = appearance;
// There are also the compactAppearance and scrollEdgeAppearance properties that can be set as needed.

如果您希望在应用程序中的所有导航栏上进行相同的自定义,请将其应用于UINavigationBar.appearance代理。

UINavigationBar.appearance.standardAppearance = appearance;
于 2019-09-25T15:54:32.167 回答
2

对我来说,即使我正确地将外观设置为当前的 UINavigationBar,它仍然无法正常工作。

我发现navigationBar?.setNeedsLayout()如果您在已显示的 UINavigationBar 上设置更新的外观,您需要调用。

于 2020-04-28T09:01:13.533 回答
-1

在 iOS13 中,您需要在 UINavigationBarAppearance 对象上设置标题颜色,如下所示(Objective C 版本):

UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
appearance.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
self.navigationController.navigationBar.standardAppearance = appearance;
self.navigationController.navigationBar.scrollEdgeAppearance = appearance;
于 2020-04-10T13:21:53.860 回答