Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 发布调整UIToolBar | Swift的高度_Ios_Swift_Uitoolbar - Fatal编程技术网

Ios 发布调整UIToolBar | Swift的高度

Ios 发布调整UIToolBar | Swift的高度,ios,swift,uitoolbar,Ios,Swift,Uitoolbar,当前,我有以下UIToolBar,当选择文本字段并显示键盘时,它会出现 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

当前,我有以下
UIToolBar
,当选择文本字段并显示键盘时,它会出现

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
我想调整
UIToolBar
的高度,使其占据更多屏幕空间,我尝试了以下方法,但似乎没有任何效果

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)
我也尝试过: bar.frame=CGRect(x:myToolbar.frame.origin.x,y: bar.frame.origin.y,宽度:myToolbar.frame.size.width,高度:20)

这两种方法似乎根本不会改变高度

更新2:

所以这个方法似乎有效,但我只希望它在键盘打开时出现,而不是视图控制器第一次打开时出现

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)

免责声明:在浏览器中键入,未在Xcode中测试

首先在
UIViewController
中为工具栏创建一个属性:

let bar:UIToolbar?

然后在
viewDidLoad
中,您可以像代码中所示那样设置栏。确保在
viewDidLoad
中添加行
myTextField.delegate=self

现在,您需要遵守
UITextFieldDelegate
协议,并响应
textfieldDiBeginediting
以显示工具栏:

扩展UIViewController:UIExtFieldDelegate{ func textField didbeginediting(textField:UITextField){ 如果textField==myTextField{ bar.ishiden=false } } }
这应该会显示工具栏,现在您可以尝试大小。您可以将其再次隐藏在类似的方法中:
textfielddidediting

这是否回答了您的问题?不,没有,请看我更新的帖子。似乎什么也没有发生尝试将
self.view.autoresizesSubviews
设置为false(
self.view
bar
的父视图)。否,相同的问题持续存在扫描是否显示将bar添加到视图中的代码?