Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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
Swift 将UIStackView添加为UIBarButtonim的自定义视图_Swift_Uikit_Uitoolbar - Fatal编程技术网

Swift 将UIStackView添加为UIBarButtonim的自定义视图

Swift 将UIStackView添加为UIBarButtonim的自定义视图,swift,uikit,uitoolbar,Swift,Uikit,Uitoolbar,我尝试添加一个UIStackView作为UIBarButtonItem的自定义视图 我首先尝试添加一个UIView作为自定义视图 let list = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 44)) list.backgroundColor = .green list.addSubview(stackView) let item = UIBarButtonItem(customView: list ) topViewContro

我尝试添加一个
UIStackView
作为
UIBarButtonItem
的自定义视图

我首先尝试添加一个
UIView
作为自定义视图

let list = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 44))
list.backgroundColor = .green
list.addSubview(stackView)
let item = UIBarButtonItem(customView: list )
topViewController?.setToolbarItems([item], animated: true)
这很有效。我在
UIToolBar
中得到一个绿色条。然后我尝试将
UIStackView
添加到
UIView

let red = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 30))
red.backgroundColor = .red
let stackView = UIStackView(frame: CGRect(origin: CGPoint.zero,
                                         size: CGSize(width: 250, height: 44)))
stackView.distribution = .fillEqually
stackView.axis = .horizontal
stackView.spacing = 5
stackView.alignment = .center
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.addArrangedSubview(red)
let list = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 44))
list.backgroundColor = .green
list.addSubview(stackView)
let item = UIBarButtonItem(customView: list )
topViewController?.setToolbarItems([item], animated: true)

然而,当我尝试这个时,什么都没有发生。
UIToolBar
似乎是空的。我做错了什么?

在这个答案中,我使用了两个
ui视图

对于两个
ui视图

red.heightAnchor.constraint(equalToConstant: 30).isActive = true;
green.heightAnchor.constraint(equalToConstant: 30).isActive = true;
你必须评论这句话

//stackView.translatesAutoresizingMaskIntoConstraints = false
完整代码:

    self.navigationController?.isToolbarHidden = false

    let red = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
    red.backgroundColor = .red
    let green = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
    green.backgroundColor = .green

    red.heightAnchor.constraint(equalToConstant: 30).isActive = true;
    green.heightAnchor.constraint(equalToConstant: 30).isActive = true;

    let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: 250, height: 30))
    stackView.distribution = .fillEqually
    stackView.axis = .horizontal
    stackView.spacing = 5
    stackView.alignment = .center
    //stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.addArrangedSubview(red)
    stackView.addArrangedSubview(green)

    let list = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 44))
    list.backgroundColor = .yellow
    list.addSubview(stackView)
    let item = UIBarButtonItem(customView: list )
    self.setToolbarItems([item], animated: true)
输出:

    self.navigationController?.isToolbarHidden = false

    let red = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
    red.backgroundColor = .red
    let green = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
    green.backgroundColor = .green

    red.heightAnchor.constraint(equalToConstant: 30).isActive = true;
    green.heightAnchor.constraint(equalToConstant: 30).isActive = true;

    let stackView = UIStackView(frame: CGRect(x: 0, y: 0, width: 250, height: 30))
    stackView.distribution = .fillEqually
    stackView.axis = .horizontal
    stackView.spacing = 5
    stackView.alignment = .center
    //stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.addArrangedSubview(red)
    stackView.addArrangedSubview(green)

    let list = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 44))
    list.backgroundColor = .yellow
    list.addSubview(stackView)
    let item = UIBarButtonItem(customView: list )
    self.setToolbarItems([item], animated: true)

为什么不使用多个项目,而不是尝试将其全部放在堆栈视图中?@LeoDabus查看它们之间的间距。我找不到一个好方法来更改
UIToolBar
中各个项目之间的间距,因此我认为这是第二个最好的解决方案。如果needed@LeoDabus嗯,我需要的空间比项目之间的默认宽度小。这就是为什么我没有运行你的代码,但对我来说似乎还可以,顺便说一句,你可以看看约束是否有效