0

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)
John
  • 965
  • 8
  • 16
  • Does this answer your question? [Is there a way to change the height of a UIToolbar?](https://stackoverflow.com/questions/2135407/is-there-a-way-to-change-the-height-of-a-uitoolbar) – koen Nov 05 '19 at 18:48
  • No it does not, please see my updated post. Nothing seems to occurs – John Nov 05 '19 at 18:54
  • Try setting `self.view.autoresizesSubviews` to false (`self.view` is the parent view of `bar`). – koen Nov 05 '19 at 19:02
  • No, the same issue persists – John Nov 05 '19 at 19:14
  • Can you show the code where you add bar to your view? – koen Nov 05 '19 at 19:16
  • I might be misunderstanding what you are saying, but I thought that is what I am doing in my post – John Nov 05 '19 at 19:17
  • What I mean is something like `myView.addSubview(bar)`. You don't show how `bar` fits in the rest of the UI. – koen Nov 05 '19 at 19:18
  • I am not doing that, everything related the UIBar is what I have posted. Do you recommend doing it a different way? – John Nov 05 '19 at 19:19
  • Well, it won't appear on the screen if you don't add it :) See also here, for instance: https://stackoverflow.com/questions/7711999/how-to-add-a-uitoolbar-programmatically-to-an-ios-app/7712240 (scroll down for Swift code). – koen Nov 05 '19 at 19:21
  • I updated the post, I used the code from the post you mentioned but because of `view.addSubview(toolBar)` it is showing the view when the view controller is first opened, can this be changed? – John Nov 05 '19 at 20:36
  • Perhaps, changing view to something else – John Nov 05 '19 at 20:36
  • Well, based on your first sentence, you want to show it when a textField becomes active and the keyboard appears, correct? In that case you could respond to `textFieldDidBeginEditing`, this is a `UITextFieldDelegate` method. – koen Nov 05 '19 at 20:42
  • Sorry can you please show how this can be done? I will mark it as correct. – John Nov 05 '19 at 21:35

1 Answers1

0

Disclaimer: typed in browser, not tested in Xcode.

First create a property in your UIViewController for the toolbar:

let bar: UIToolbar?

Then in viewDidLoad you can set up the bar like you shown in your code. Make sure that you also add the line myTextField.delegate = self in your viewDidLoad.

Now you need to conform to the UITextFieldDelegate protocol and respond to textFieldDidBeginEditing to show the toolbar:

extension UIViewController: UITextFieldDelegate {
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if textField == myTextField {
            bar.isHidden = false
        }
    }
}

That should show the toolbar and now you can experiment with the size. You can hide it again inside a similar method: textFieldDidEndEditing.

koen
  • 5,383
  • 7
  • 50
  • 89