Swift 设置UIBarButtonItem的字体大小
我正在用以下代码创建一个Swift 设置UIBarButtonItem的字体大小,swift,uibarbuttonitem,Swift,Uibarbuttonitem,我正在用以下代码创建一个uibarbuttonim let rightItem = UIBarButtonItem(title: "➞", style: UIBarButtonItemStyle.Plain, target: self, action: "navigateToViewTwo") if let font = UIFont(name: "System", size: 25.0) { rightItem.setTitleTextAttributes([
uibarbuttonim
let rightItem = UIBarButtonItem(title: "➞", style: UIBarButtonItemStyle.Plain, target: self, action: "navigateToViewTwo")
if let font = UIFont(name: "System", size: 25.0) {
rightItem.setTitleTextAttributes([NSFontAttributeName: font], forState: UIControlState.Normal)}
navigationController?.navigationBar.topItem?.rightBarButtonItem = rightItem
该按钮应显示一个右箭头,确实如此。问题是字体无法控制按钮的大小。我得到以下信息:
只有在使用系统字体时才会发生错误。我用Helvetica试过了,结果如下:
箭头肯定更大,但导航栏上的箭头也太高。如果我旋转屏幕,它看起来很糟糕
箭头太高,看起来不合适。与左侧的“项目”按钮相比,看到它的外观了吗?那一个被拖拽掉了
如何调整箭头,使其处于正确的大小和屏幕上的正确位置?我以前遇到过此问题,我决定使用自定义视图来处理此问题,如下所示:
var view = // create your custom view
var btnMenu = UIBarButtonItem(customView: view)
必须设置标题边缘插入 与CustomView 和你一样正常 将两者都设置为比较,只需添加看起来更好的:
navigationController?.navigationBar.topItem?.rightBarButtonItems = [rightItem,right]
结果:
希望它能对你有所帮助。这对右翼议员很有效,但对后座议员根本不起作用。backBarItem不受字体设置的影响。它的大小保持不变。要使所有按钮都能使用,还需要做些什么吗?@ScottKilbourn如果您想在左侧添加按钮,则有
leftBarButtonItems
。@ScottKilbourn如果您只想设置一个按钮,则rightBarButtonItem
或leftBarButtonItem
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor(red:0.12, green:0.28, blue:0.40, alpha:1.0), NSAttributedStringKey.font: UIFont(name: "GalanoGrotesque-SemiBold", size: 18)!], for: .normal)
navigationController?.navigationBar.topItem?.rightBarButtonItems = [rightItem,right]
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor(red:0.12, green:0.28, blue:0.40, alpha:1.0), NSAttributedStringKey.font: UIFont(name: "GalanoGrotesque-SemiBold", size: 18)!], for: .normal)