在我的代码中,我创建了一个 for 循环,它为我的数组中的所有 17 个项目创建一个 uibarbuttonitem,就像这样。 我的数组是
let musicalnotes = ["C", "C♯", "D", "D♯", "E", "F", "F♯", "G", "G♯", "A", "A♯", "B", "E♭", "E","G♭", "A♭", "B♭"]
func createnotes(){
let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
// toolBar.frame = CGRect(x: toolBar.frame.origin.x, y: toolBar.frame.origin.y, width: toolBar.frame.size.width, height: 900)
var buttonarray = [UIBarButtonItem]()
for a in musicalnotes {
let a = UIBarButtonItem(title: a, style: .plain, target: self, action: #selector(self.addnotetonote(sender:)))
buttonarray.append(a)
}
let cancel = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(self.revert))
buttonarray.append(cancel)
toolBar.isTranslucent = true
toolBar.items = buttonarray
我的问题是 iPhone 上的工具栏显示的按钮如下所示:
在 iPhone 上,按钮不可见但可以使用,但在 iPad 上是正常的。
是因为工具栏上放不下的按钮太多还是别的原因?
最佳答案
将 toolBar 添加到 UIScrollView 中。
scrollView.addSubview(toolBar)
并且会推荐使用自动布局
toolBar.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
toolBar.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
toolBar.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
toolBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
关于ios - swift uitoolbar 没有显示所有的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52080273/