设置UIBarButtonItem的字体大小

Sco*_*urn 1 uibarbuttonitem swift

我正在UIBarButtonItem使用以下代码创建一个.

    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
Run Code Online (Sandbox Code Playgroud)

按钮应显示右箭头,确实如此.问题是字体不控制按钮的大小.我得到以下内容:

在此输入图像描述

使用系统字体时唯一发生.我和Helvetica一起尝试过,我得到了以下内容:

在此输入图像描述

箭头肯定更大,但导航栏上的箭头也太高了.如果我旋转屏幕,它看起来很糟糕.

在此输入图像描述

箭头太高,看起来不合适.与左侧的项目按钮相比,看看它的外观如何?那个人被拖了下来.

如何调整箭头以使其尺寸正确,并且位于屏幕上的正确位置?

Ash*_*kad 10

您必须设置标题边缘插入

使用CustomView

var btn = UIButton.buttonWithType(UIButtonType.System) as! UIButton
btn.frame = CGRectMake(10, 20, 50, 50)
btn.setTitle("?", forState: UIControlState.Normal)
btn.titleLabel?.font = UIFont(name: "Helvetica" , size: 17)
btn.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 0, 0)
btn.addTarget(self, action: Selector("navigateToViewTwo"), forControlEvents: UIControlEvents.TouchUpInside)
var right = UIBarButtonItem(customView: btn)
Run Code Online (Sandbox Code Playgroud)

正如你所做的那样

var rightItem = UIBarButtonItem(title: "?", style: UIBarButtonItemStyle.Plain, target: self, action: "navigateToViewTwo")
rightItem.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Helvetica", size: 17.0)!], forState: UIControlState.Normal)
rightItem.setTitlePositionAdjustment(UIOffsetMake(0.0, 5.0), forBarMetrics: UIBarMetrics.Default)
Run Code Online (Sandbox Code Playgroud)

设置两者进行比较,只需添加看起来像面糊:

navigationController?.navigationBar.topItem?.rightBarButtonItems = [rightItem,right]
Run Code Online (Sandbox Code Playgroud)

结果:

在此输入图像描述

希望它会对你有所帮助.


Ale*_*rla 6

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)
Run Code Online (Sandbox Code Playgroud)