ios - 更改 UIToolbar 项目导致工具栏变为空白

标签 ios swift uibarbuttonitem uitoolbar

我在 Storyboard 中创建了一个自定义 UIToolbar,其中包含快退、暂停和快进 UIBarButtonItems。单击时,我试图用播放按钮替换暂停按钮。我的代码如下:

@IBOutlet weak var bottomToolbar: UIToolbar!

@IBAction func playPause() {
    var newButton: UIBarButtonItem
    if !self.timer.valid {
        let aSelector : Selector = "updateTime"
        self.timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: aSelector, userInfo: nil, repeats: true)
        newButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause")
    }
    else {
        self.timer.invalidate()
        newButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause")
    }
    var items = self.bottomToolbar.items
    items?[3] = newButton
    self.bottomToolbar.setItems(items, animated: true)
}

工具栏初始化为带有白色按钮的深灰色背景。然而,在尝试按钮切换之后,整个工具栏变白了。有什么想法吗?

更新 1:
因此,在弄乱颜色并多步执行代码之后,我发现除了工具栏变白之外,只有暂停/播放按钮消失了。但即使在消失之后,点击它应该在的地方仍然会向 VC 发送信号。尝试将工具栏背景重置为深灰色也无济于事。

最佳答案

我最近遇到了类似的问题。有用的是将 toolbar.Items 设置为一个空数组,然后再将其再次设置为您的项目。

我的 swift 非常有限,所以请原谅任何编译错误:

 items = self.bottomToolbar.items
 self.bottomToolbar.setItems:([], animated: false)
 items?[3] = newButton
 self.bottomToolbar.setItems(items, animated: true)

关于ios - 更改 UIToolbar 项目导致工具栏变为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29176364/

相关文章:

ios - Apns代表从未被解雇,为什么我需要关闭网络和wifi

ios - SwiftUI 中的@Binding 和 ForEach

ios - 错误 : Cannot convert value of type '(_, _) -> Void' to expected argument type '((UIAlertAction) -> Void)?'

iOS - 更改 UIBarButtonItem 的高度

ios - UIButton 未在 iOS 中显示

objective-c - IOS UIBarButtonItem 工作正常而 UIButton 导致无法识别的选择器发送到实例

ios - 在 Apple Watch 中管理 Glance 界面

iphone - uiscrollview 内容大小的较大值是否会影响设备内存的分配?

c++ - OpenCV 3.0 问题 - 录制视频内存泄漏

swift - 为什么 MQTT 在 swift 上连接到主机后总是断开连接