【问题标题】:why I cannot change the font of my back button in UINavigationController?为什么我无法在 UINavigationController 中更改后退按钮的字体?
【发布时间】:2017-03-04 17:35:07
【问题描述】:

我想更改导航控制器的文本和字体。

我有一个静态的UITableView,当用户单击一个单元格时 - 我通过 segue 转到第二个 UIViewController。

我在故事板和我拥有的代码中设置了转场的名称:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if (segue.identifier == "myTestSegue") {
        if segue.destination is MyTest {
            print("can I see it?")
        let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: nil)
        backButton.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AppleSDGothicNeo-Light", size: 20.0)!], for: UIControlState())
        navigationItem.backBarButtonItem = backButton
        }
    }
}

现在,当我运行应用程序并单击单元格时,我在控制台中看到:can I see it?,但下一个面板上按钮的文本和字体没有改变。

我错过了什么?

【问题讨论】:

  • 我用相同的代码设置了完全相同的情况,它对我有用。所以你在上面解释的事情之外发生了一些事情。

标签: ios swift uinavigationcontroller uinavigationbar uistoryboardsegue


【解决方案1】:

试试这个代码;

    let customFont = UIFont(name: "Your font Name", size: 17.0)
    UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont!], for: UIControlState.normal)

【讨论】:

    【解决方案2】:

    您可以使用此代码更改后退按钮的字体样式。

    [[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
     setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor],
       NSShadowAttributeName:shadow,
       NSFontAttributeName:[UIFont boldSystemFontOfSize:15.0]
       }
     forState:UIControlStateNormal];
    

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 2014-01-31
      • 2012-12-27
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 2015-10-11
      • 2012-04-09
      • 1970-01-01
      相关资源
      最近更新 更多