如何在iOS 13中使用UINavigationBarAppearance [英] How to use UINavigationBarAppearance in iOS 13

查看:2033
本文介绍了如何在iOS 13中使用UINavigationBarAppearance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do you use this new object to customize the navigation bar in iOS 13? I tried the following code in Objective-C but it's not working correctly. It only shows my title text attributes while a view controller is being pushed or popped on to the navigation stack.

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

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

Here is the documentation for this object. https://developer.apple.com/documentation/uikit/uinavigationbarappearance?language=objc

推荐答案

仅创建UINavigationBarAppearance的实例是不够的.您实际上必须在UINavigationBar实例(或其外观代理)上进行设置.

It isn't enough to just create an instance of UINavigationBarAppearance. You have to actually set it on a UINavigationBar instance (or its appearance proxy).

// 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代理.

If you want this same customization on all nav bars in the app, apply it to the UINavigationBar.appearance proxy.

UINavigationBar.appearance.standardAppearance = appearance;

这篇关于如何在iOS 13中使用UINavigationBarAppearance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆