【发布时间】:2023-03-21 01:22:01
【问题描述】:
我在 Storyboard 中创建了一个自定义 UIToolbar,其中包含倒带、暂停和快进 UIBarButtonItem。单击时,我试图用播放按钮替换暂停按钮。我的代码如下:
@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 发送信号。并且尝试将工具栏背景重置为深灰色也无济于事。
【问题讨论】:
-
单步执行代码时,最后一行之前
items的内容是什么?是否符合预期? -
@Mundi 是的,据我所知,
items数组包含所有正确的 UIBarButtonItems。总共有 7 个 - 灵活空间、倒带、灵活空间、播放/暂停、灵活空间、快进和灵活空间。倒带和快进项目具有正确的动作和目标,所以我认为数组的其余部分是好的。 -
嗯,工具栏代码看起来不错。您可以尝试发送
setNeedsDisplay。这个问题很可能在其他地方被发现。 -
setNeedsDisplay没用 :( 谢谢,我会尝试寻找其他地方 -
你解决过这个问题吗?
标签: ios swift uibarbuttonitem uitoolbar