简体   繁体   中英

Issue adjusting the height of UIToolBar | Swift

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM