1

I added a toolbar with done and cancel buttons and everything was right. Suddenly, the done and cancel text is hidden. The buttons exist and are clickable but with no text. I tried everything but same problem.

This is the code regarding the toolbar:

func createDatePicker(){
    //format for datepicker display
    datePicker.datePickerMode = .date
    datePicker.minimumDate = Date()
    datePicker.backgroundColor = UIColor.white

    // ToolBar
    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.backgroundColor = UIColor(red: 253/255, green: 184/255, blue: 20/35, alpha: 1.0)

    //datePicker.tintColor = UIColor.blue
    toolBar.tintColor = UIColor.blue
    toolBar.sizeToFit()
    toolBar.isUserInteractionEnabled = true
    //add a done button on this toolbar

    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelClicked))
    toolBar.setItems([cancelButton,spaceButton,doneButton], animated: true)
    testfield.inputAccessoryView = toolBar
    testfield.inputView = datePicker

    self.view.addSubview(testfield)
}

And this is the result:

and this is the result

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You can try to figure where your buttons are gone with the hierarchy inspector: follow this → https://stackoverflow.com/a/26052806/1392046 – Crazyrems Jun 26 '18 at 11:44

2 Answers2

1

Here is the Code is am using based on your code and it is working perfectly fine for me

import UIKit

class ViewController: UIViewController {

    var datePicker = UIDatePicker()
    var testfield: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()
        createDatePicker()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    func createDatePicker(){
        // TextField
        testfield = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
        testfield.placeholder = "testfiled"
        self.view.addSubview(testfield)

        //format for datepicker display
        datePicker.datePickerMode = .date
        datePicker.minimumDate = Date()
        datePicker.backgroundColor = UIColor.white

        // ToolBar
        let toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.default
        toolBar.isTranslucent = true
        toolBar.backgroundColor = UIColor(red: 253/255, green: 184/255, blue: 20/35, alpha: 1.0)

        //datePicker.tintColor = UIColor.blue
        toolBar.tintColor = UIColor.blue
        toolBar.sizeToFit()
        toolBar.isUserInteractionEnabled = true
        //add a done button on this toolbar

        let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneClicked))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(cancelClicked))
        toolBar.setItems([cancelButton,spaceButton,doneButton], animated: true)
        testfield.inputAccessoryView = toolBar
        testfield.inputView = datePicker

    }

    @objc func doneClicked(){

    }
    @objc func cancelClicked(){

    }

}

Please try to test on both Device And Simulator Or try clean(shift+cmd+k) project before build this helps in most of cases.

Result

enter image description here

Rizwan Mehboob
  • 1,333
  • 17
  • 19
  • I Tried this code outside my project and it works fine ... i then added another VC in my project with the code but it still with the same issue ... there is something wrong with in my project causing this issue but i cant figure out where – Ahmed Samir Jun 25 '18 at 08:01
  • I think some thing else is hindering this to appear can I ask you to share the project? – Rizwan Mehboob Jun 25 '18 at 08:14
  • Fully or partially according to your convenience. – Rizwan Mehboob Jun 25 '18 at 13:05
  • I solved this issue by a working around ,,,,making custom point ,,,,, adding image to the button (see my comment) – Ahmed Samir Jun 26 '18 at 11:37
  • I am glad its fixed your issue partially, but it is not a solution to your question, I would sagest to figure out what is causing actual issue because it can happen some where else your code base also. – Rizwan Mehboob Jun 26 '18 at 11:47
0

I did a WORK AROUND by adding image to the done and cancel button

let DonebuttonIcon = UIImage(named: "done-calender.png")
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(doneClicked))
    doneButton.image = DonebuttonIcon
    doneButton.tintColor = UIColor.gray

    let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

    let CancelbuttonIcon = UIImage(named: "cancel-calender.png")
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.done, target: self, action: #selector(cancelClicked))
    cancelButton.image = CancelbuttonIcon
    cancelButton.tintColor = UIColor.gray