Currently I have the following UIToolBar
that appears when a textfield is selected and a keyboard appears
let bar = UIToolbar()
let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let reset = UIBarButtonItem(title: "Tool Bar Text", style: .plain, target: self, action: #selector(functionExample))
reset.tintColor = UIColor.white
bar.barTintColor = UIColor.red
bar.items = [spacer,reset, spacer]
bar.sizeToFit()
exampleTextField = bar
I would like to adjust the height of the UIToolBar
to make it take up more screen, I have tried the following but it seems to not do anything.
bar.frame = CGRect(x: bar.frame.origin.x, y: bar.frame.origin.y, width: bar.frame.size.width, height: 150)
bar.frame = CGRect(x: 0, y: view.frame.size.height - 80, width: view.frame.size.width, height: 80)
I have also tried: bar.frame = CGRect(x: myToolbar.frame.origin.x, y: bar.frame.origin.y, width: myToolbar.frame.size.width, height: 20)
Both methods seem to not change the height at all.
UPDATE 2:
So this method seems to work, but I only want it to appear when the keyboard comes up, not when the view controller first opens,
let toolBar = UIToolbar()
var items = [UIBarButtonItem]()
items.append(
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
items.append(
UIBarButtonItem(title: "Tool Bar Text", style: .plain, target: self, action: #selector(confirmSignature))
)
items.append(
UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
)
toolBar.setItems(items, animated: true)
toolBar.tintColor = .white
toolBar.barTintColor = UIColor.red
toolBar.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
let guide = self.view.safeAreaLayoutGuide
toolBar.trailingAnchor.constraint(equalTo: guide.trailingAnchor).isActive = true
toolBar.leadingAnchor.constraint(equalTo: guide.leadingAnchor).isActive = true
toolBar.bottomAnchor.constraint(equalTo: guide.bottomAnchor).isActive = true
toolBar.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
else {
NSLayoutConstraint(item: toolBar, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: toolBar, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
NSLayoutConstraint(item: toolBar, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
}
exampleTextField.inputAccessoryView = toolBar
view.addSubview(toolBar)