我有我的导航栏,我没有设置字体并保留默认值。
根据我的研究,它必须在navigationBar.titleTextAttributes
,但我不知道如何从titleTextAttributes获取fontattributes。
如何获取导航栏的fontsize和fontname?
var navigationBar = UINavigationBar(frame: CGRectMake(0, 0, 400, 40));
navigationItem.title = "my Title";
let fontsize=navigationBar.titleTextAttributes.????????
更新:我想做什么?
我在模态模式(popover)中调用ViewController来显示一个较小的对话框。该对话框有一个导航栏。通过代码创建导航栏时:
var navigationBar = UINavigationBar();
let navigationItem = UINavigationItem()
navigationItem.title = sBookmarktitle;
// Create left and right button for navigation item
let leftButton = UIBarButtonItem(title: sBacklabel, style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
let rightButton = UIBarButtonItem(title: saddBookmark, style: UIBarButtonItemStyle.Plain, target: self, action: "addBookmarkAction:")
// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem];
self.view.addSubview(navigationBar);
并像这样调用ViewController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Bookmarks") as! BookmarkViewController;// UIViewController
vc.parentviewname=callerName;
vc.modalPresentationStyle = UIModalPresentationStyle.Popover
let popoverPresentationController = vc.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {
// set the view from which to pop up
_popoverPresentationController.sourceView = actualViewController.view;
_popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
actualViewController.presentViewController(vc, animated: true, completion: nil)
}
然后它看起来像这样:
正如您所看到的,导航栏看起来不是很好:-)。所以我尝试手动设置大小:
navigationBar = UINavigationBar(frame: CGRectMake(0, 0, fDialogWidth, navbarHeigth))
我尝试计算导航栏的最小宽度。我的方法是计算我的栏中所有文本大小,其间有以下几个空格: 标题+左侧链接(“后退”)+右侧链接(“添加”)。 为此,我需要fontsize和fonttype。
这种做法可能不是最佳做法吗?
答案 0 :(得分:0)
不要将导航栏作为子视图添加到视图控制器。完全相反:将视图控制器作为子视图控制器添加到具有导航栏的导航控制器!像这样:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc2 = storyboard.instantiateViewControllerWithIdentifier("Bookmarks") as! BookmarkViewController;// UIViewController
vc2.parentviewname=callerName
let vc = UINavigationController(rootViewController:vc2)
现在,您的视图控制器(BookmarkViewController)照常配置自己的navigationItem
。