0

I have added a bar button item programmatically that will change the temperature units on some variables between Celsius and Fahrenheit and have set it with attributed text via a customView.

It is half Bold and half standard text. Whichever unit is selected will be bold.

After trying each method of refreshing them I would remove them after they didn't work. So in the code below there is no "reload/refresh" being attempted.

enter image description here

I have a boolean which changes when the action is called. However I cannot get the barButtomItem to "Reload/refresh" upon being selected.

I have debugged and the action is being called, the variable is changing, and the formatted text is being changed, however it is not changing on the screen.

//MARK: - Create Navigation Bar
func createNavBar() {

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.backgroundColor = .clear
    navigationController?.navigationBar.isTranslucent = true

    let CFLabel = UILabel()
    CFLabel.attributedText = formatCFButton()
    CFLabel.sizeToFit()
    CFLabel.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(CFChanger))
    CFLabel.addGestureRecognizer(tap)


    let CFButton = UIBarButtonItem.init(customView: CFLabel)
    let addCityButton = UIBarButtonItem.init(barButtonSystemItem: .add, target: self, action: #selector(addCity))
    addCityButton.tintColor = .black

    self.navigationItem.rightBarButtonItem = addCityButton
    self.navigationItem.leftBarButtonItem = CFButton

}
   @objc func CFChanger() {
    print("CF BUTTON PRESSED AND FUNCTION ACTIVATED!")
    isCelsius = !isCelsius
}


//Change font(bold) change temperature units button
func formatCFButton() -> NSMutableAttributedString {

    let changeTempString = NSMutableAttributedString()
    let boldAttribute = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Bold", size: 18.0)!]
    let regularAttribute = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 18.0)!]

    if isCelsius == true {
        let boldText = NSAttributedString(string: "C ", attributes: boldAttribute)
        let regularText = NSAttributedString(string: "/ F", attributes: regularAttribute)
        changeTempString.append(boldText)
        changeTempString.append(regularText)
    } else {
        let boldText = NSAttributedString(string: "F ", attributes: boldAttribute)
        let regularText = NSAttributedString(string: "C / ", attributes: regularAttribute)
        changeTempString.append(regularText)
        changeTempString.append(boldText)
    }
    return changeTempString
}

I was able to get this to work in the past when I did NOT create the button programmatically by adding this code into the function when the button is pressed. I am not sure how to do this programmatically. (sender stuff)

// MARK: - Change units function
//****************************************************************************
@IBAction func changeUnitsButtonPressed(_ sender: UIButton) {
    isCelsius = !isCelsius
    var tempUnitNumber = 0
    if isCelsius == true {
        tempUnitNumber = 1
    } else {
        tempUnitNumber = 2
    }

    defaults.set(tempUnitNumber, forKey: "tempUnit")
    let changeTempString = formatCFButton()
    sender.setAttributedTitle(changeTempString, for: [])
    tableView.reloadData()
}
BlakePat
  • 75
  • 7
  • I don't understand. You are on click doing `isCelsius = !isCelsius`. But where do you redo the button? – Larme Jan 07 '20 at 09:50
  • I have tried a bunch of different ways, but none of them worked so I removed them after trying each one. So there is currently nothing there, is there a way you recommend trying? – BlakePat Jan 07 '20 at 23:02

0 Answers0