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嗯,我需要的空间比项目之间的默认宽度小。这就是为什么我没有运行你的代码,但对我来说似乎还可以,顺便说一句,你可以看看约束是否有效