ios - swift uitoolbar 没有显示所有的按钮?

标签 ios swift

在我的代码中,我创建了一个 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 example

在 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

enter image description here

关于ios - swift uitoolbar 没有显示所有的按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52080273/

相关文章:

ios - 如何在 iOS 模拟器中重置推送通知权限?

iphone - 随着音乐应用程序播放歌曲播放无声视频

ios - AppDelegate 中需要任何用于第二个核心数据实体的代码吗?

ios - 符合协议(protocol)和类的 Swift 属性

ios - Alamofire 模块不使用 Swift 3.0.1 编译

ios - 在 Google Maps SDK for native iOS/objective C 的 InfoWindow/Marker 上添加点击事件

ios - 在ios中从amazon s3下载文件

ios - Swift - 标签未在 WatchKit 中从委托(delegate)回调中更新

ios - 在 SceneKIT 中,如何向具有 SCNPlane() 几何形状的 SCNNode() 添加 Material ?

objective-c - 屏幕上特定位置点击事件的手势识别器