Making rightBarButtonItem bold - swift

I am trying to set up a navigation button to appear red and semibold. I managed to changed the color but I'm having trouble changing it to semibold:
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Sign Up", style: .plain, target: self, action: #selector(signIn))
navigationItem.rightBarButtonItem?.tintColor = .red
UIBarItem.appearance().setTitleTextAttributes(
[
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12, weight: .semibold)
],
for: .normal)
I tried rightBarButtonItem.appearance first but that doesn't seem to be an option.
I'm working with Swift 4.2.

You can set the style property to .done to make the UIBarButtonItem bold. This will work until Apple changes how the done button looks in a newer version of iOS.
navigationItem.rightBarButtonItem?.style = .done

Well to have fully customized bar button appearance, you can use bar button items with custom view just like this
let doneButton: UIButton = UIButton (type: UIButton.ButtonType.custom)
doneButton.setTitle("Done", for: .normal)
doneButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
doneButton.setTitleColor(.blue, for: .normal)
doneButton.addTarget(self, action: #selector(self.doneBarButtonTapped(sender:)), for: UIControl.Event.touchUpInside)
doneButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let barButton = UIBarButtonItem(customView: doneButton)
navigationItem.rightBarButtonItem = barButton

Related

NavigationBarButton Item Not able to align vertically?

Hi I have tried to align the navigationbar button item position center in the navigation bar. I have changed the navigation bar height programmatically so I have able to align title its working fine, but same I have tried leftbarbutton and rightbarbutton. its has not vertically aligned center. I have tried these programmatically
Title Alignment
self.navigationController!.navigationBar.setTitleVerticalPositionAdjustment(-15, forBarMetrics: .Default)
RightBarButton:
let button: UIButton = UIButton(type: .Custom)
button.setImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(self.toggleRight), forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 40, 40)
let rightButton = UIBarButtonItem(customView: button)
// rightButton.imageInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10;
self.navigationItem.rightBarButtonItems = [negativeSpacer ,rightButton ]
LeftBarButton:
let button: UIButton = UIButton(type: .Custom)
button.setImage(buttonImage, forState: UIControlState.Normal)
button.addTarget(self, action: #selector(self.toggleLeft), forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 40, 40)
let leftButton = UIBarButtonItem(customView: button)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -10;
self.navigationItem.leftBarButtonItems = [negativeSpacer,leftButton ]
Please find the screenshot.

How can i move a button in a UINavigation bar to the right

im trying to move the right button in my navigation bar to the right to align it with buttons on a table view here is what is currently looks like
i made a button like this:
let allButton = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
allButton.layer.borderColor = Constants.AppColor.cgColor
allButton.layer.borderWidth = 1
allButton.layer.backgroundColor = UIColor.white.cgColor
allButton.addTarget(self, action: #selector(selectAllContacts), for: .touchUpInside)
allButton.layer.cornerRadius = 10
and im adding it to my nav bar like this:
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: allButton), importButton]
I have tried adding image insets, content insets and it doesn't work.
Any idea on what else i can try?
Will this do?
navigationItem.rightBarButtonItem = UIBarButtonItem(
title: "Menu >",
style: .plain,
target: self,
action: #selector( rightNavTap )
)
I was able to solve this by adding
let negativeSpacer:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -10
and appending that to my array of buttons
navigationItem.rightBarButtonItems = [negativeSpacer, UIBarButtonItem(customView: allButton), importButton]
If anyone finds a better solution let me know.

Changing font color of UIBarButtonItem

I tried to change the font color of the right bar button item to purple, but it still shows up as white. I've consulted this question and this question. How do I fix this?
Code
let sortButton = UIButton(frame: CGRect(x: 0, y: 0, width: 34, height: 15))
sortButton.setTitle("SORT", for: .normal)
sortButton.titleLabel?.tintColor = UIColor.myMusicPurple
sortButton.tintColor = UIColor.myMusicPurple
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sortButton)
navigationItem.rightBarButtonItem?.tintColor = UIColor.myMusicPurple
This should do the trick (if you have plain text)
let rightBarButtonItem = UIBarButtonItem(title: "Some text", style: .plain, target: self, action: #selector(someAction))
rightBarButtonItem.tintColor = UIColor.myMusicPurple
navigationItem.rightBarButtonItem = rightBarButtonItem
Please try this
sortButton.setTitleTextAttributes([NSAttributedStringKey.foregroundColor : UIColor.white], for: .normal)
Its simple, just create a reference for UIBarButtonItem from Main.stroyboard to corresponding swift file like this,
#IBOutlet var yourBarBtn: UIBarButtonItem!
After that write this line,
yourBarBtn.tintColor = .white //your_color
Thats it!
First, you need to set plain properties then after you can write as like.
#IBOutlet weak var btnGenerate: UIBarButtonItem!
btnGenerate.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.colorBlue], for: .normal)
What about using:
func setTitleColor(UIColor?, for: UIControlState)
Documentation says it sets the color of the title to use for the specified state.
sortButton.setTitleColor( .red, for: .normal)

Removing a UILabel programmatically after a button is pressed

I am trying to create/show a label when a button is pressed, and then delete/hide the same label when the same button is pressed again. I am trying to do this all programmatically in Swift.
I have tried using label.removeFromSuperview() but it doesn't seem to have any effect. However when i try removing the button in the same code location using button.removeFromSuperview()
var label = UILabel()
let labelImage = UIImage(named: "Strike Line.png")
/* to select checkmarked state */
func pressCheck() {
let image = UIImage(named: "Checkmark.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressUnCheck), for: .touchUpInside)
self.view.addSubview(button)
textField1.textColor = UIColor.gray //change textfield to a gray color
label = UILabel(frame: CGRect(x : 31, y : 69, width: 200, height: 2))
label.backgroundColor = UIColor(patternImage: labelImage!)
self.view.addSubview(label)
}
func pressUnCheck()
{
let image = UIImage(named: "To Be Completed Circle.png")
button.setBackgroundImage(image, for: UIControlState.normal)
button.addTarget(self, action:#selector(self.pressCheck), for: .touchUpInside)
self.view.addSubview(button)
label.removeFromSuperview()
textField1.textColor = UIColor.black
}
Here is where i am trying to remove/hide the label.
Since this apparently was the fix I'll drop it in as an answer.
Add button.removeTarget(nil, action: nil, for: .allEvents) before you add any new targets to your button.
If you don't remove the current target it'll have multiple targets and be calling both pressCheck() and pressUnCheck() on each button press.
There's a few ways to handle this... If you just want to hide it you can use
label.isHidden = true - would hide the label.
label.isHidden = false - would show the label.

Setting font size of UIBarButtonItem

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
With 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)
With Normal as you have done
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.
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)
I have encounter this problem before, and I decide to use a custom view to handle this as follow:
var view = // create your custom view
var btnMenu = UIBarButtonItem(customView: view)