2
我想为我的应用中的导航栏使用自定义titleView,以便它可以是用户可编辑的。我试图得到默认的字体/对齐/等,所以我可以将它应用到我的新视图,所以它看起来像一个正常的导航栏标题。获取UINavigationBar的默认titleTextAttributes
我使用
NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithDictionary:self.navigationController.navigationBar.titleTextAttributes];
NSLog(@"%@", myDict);
但这只是显示一个空的字典。我已经看过其他答案,这表明这应该返回默认字体。
我做错了什么?或者在iOS7中改变了?
编辑:由于以下卡格拉,我已经更新了我的代码:
// Get the default title label
UILabel *navigationTitleLabel;
for (UIView *sv in self.navigationController.navigationBar.subviews)
{
if (sv.subviews.count > 0 && [sv.subviews[0] isKindOfClass:[UILabel class]])
{
navigationTitleLabel = (UILabel *)sv.subviews[0];
break;
}
}
// Set the new text field to have the same attributes as the default title label
textField.font = navigationTitleLabel.font;
textField.textColor = navigationTitleLabel.textColor;
textField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = textField;
非常感谢您的帮助。我用你的代码在导航栏中找到UILabel,然后直接从那里获取字体属性,并将它们放在我的新视图(这是一个UITextField)。 – Frakur