I am creating a UIBarButtonItem
with the following code.
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
The button should display a right arrow, and it does. The problem is that the font is not controlling the size of the button. I get the following:
The only happens when using the System font. I tried it with Helvetica, and I got the following:
The arrow is definitely larger, but it is also too high on the nav bar. If I rotate the screen, it looks bad.
The arrow is too high, and looks out of place. See how it looks, compared to the Item button on the left? That one was dragged and dropped.
How can I adjust the arrow so that it is in the correct size, and in the correct place on the screen?
You have to set title edge insets
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)
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)
Set both for comparison, just add it which looks batter :
navigationController?.navigationBar.topItem?.rightBarButtonItems = [rightItem,right]
Results :
Hope it will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With