首页 > 解决方案 > 无法设置我的导航栏标题的字体大小

问题描述

我试图弄清楚如何使用 StackOverflow 做到这一点并做到了这一点:

[self.navController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont.systemFontSize:17 weight: UIFontWeightSemibold]}];

不幸的是,我收到一个错误,下划线UIFont.systemFontSize显示Bad receiver type 'CGFloat'(aka 'double').

如何调整我的代码以使其发挥作用?

标签: iosobjective-c

解决方案


您的语法已关闭

+ (UIFont *)systemFontOfSize:(CGFloat)fontSize weight:(UIFontWeight)weight NS_AVAILABLE_IOS(8_2);

https://developer.apple.com/documentation/uikit/uifont/1619027-systemfontofsize?language=objc

您想在此处删除句点UIFont.systemFontSize,而是使用UIFont systemFontOfSize:weight:

与其在设置属性时尝试将所有内容内联,不如将它们分开有时会有所帮助,即:

    UIColor *whiteColor = [UIColor whiteColor];
    UIFont *navBarTitleFont = [UIFont systemFontOfSize:17.0f weight:UIFontWeightSemibold];
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSForegroundColorAttributeName: whiteColor,
                                                                      NSFontAttributeName: navBarTitleFont
                                                                      }];

推荐阅读