为什么我不能在 UINavigationController 中更改后退按钮的字体?
why I cannot change the font of my back button in UINavigationController?
我想更改导航控制器的文本和字体。
我有一个静态 UITableView
,当用户单击一个单元格时 - 我通过 segue 转到第二个 UIViewController。
我在情节提要和代码中设置了 segue 的名称:
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?
,但下一个面板上按钮的文本和字体没有改变。
我错过了什么?
您可以使用此代码更改后退按钮的字体样式。
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont boldSystemFontOfSize:15.0]
}
forState:UIControlStateNormal];
试试这个代码;
let customFont = UIFont(name: "Your font Name", size: 17.0)
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont!], for: UIControlState.normal)
我想更改导航控制器的文本和字体。
我有一个静态 UITableView
,当用户单击一个单元格时 - 我通过 segue 转到第二个 UIViewController。
我在情节提要和代码中设置了 segue 的名称:
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?
,但下一个面板上按钮的文本和字体没有改变。
我错过了什么?
您可以使用此代码更改后退按钮的字体样式。
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor whiteColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont boldSystemFontOfSize:15.0]
}
forState:UIControlStateNormal];
试试这个代码;
let customFont = UIFont(name: "Your font Name", size: 17.0)
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont!], for: UIControlState.normal)