我有一个导航栏按钮项,它在数据查询期间添加了一个自定义活动指示器视图。查询完成后,我想将按钮设置回其原始状态。这是我当前的代码:
let filterActivityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
filterActivityIndicator.color = .yandasRed
filterMenuNavButton.customView = filterActivityIndicator
filterActivityIndicator.startAnimating()
ProductsFilterData.searchShared.updateFilteredCollectionData(source: "Search") { (done) in
filterActivityIndicator.stopAnimating()
self.filterMenuNavButton.setBackgroundImage(#imageLiteral(resourceName: "icons8-slider-30"), for: .normal, barMetrics: .default)
}
我试过将自定义背景设置为nil,但没有任何效果。我试过重新设置条按钮项的背景图像,但没有任何效果。有什么想法吗?谢谢!
编辑:
我要操作的uibarbuttonem在右边,但是右边有两个按钮项。
filterMenuNavButton
是我试图操作的特定按钮的IBOutlet。 最佳答案
有时无效是个问题。似乎您需要再次将按钮设置到导航栏。它是如此的聪明,以至于你首先需要将它设置为nil
,这样效果才会发生。
这就是我试过的:
let buttonImage: UIImage = #imageLiteral(resourceName: "icons8-slider-30")
let filterMenuNavButton = UIBarButtonItem(image: buttonImage, style: .plain, target: nil, action: nil)
self.navigationItem.rightBarButtonItem = filterMenuNavButton
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let filterActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
filterMenuNavButton.customView = filterActivityIndicator
filterActivityIndicator.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
filterActivityIndicator.stopAnimating()
filterMenuNavButton.customView = nil
filterMenuNavButton.setBackgroundImage(buttonImage, for: .normal, barMetrics: .default)
self.navigationItem.rightBarButtonItem = nil
self.navigationItem.rightBarButtonItem = filterMenuNavButton
}
}
我不确定您的设置是什么,但我使用了一个简单的导航视图控制器,并将此代码放入当前(根)视图控制器的
viewDidLoad
中。最后一部分可能是你想要的,你需要:
将自定义视图设置为清除
添加图像或更改其他按钮属性
清除按钮项(设置为零)
将按钮项设置回所需项
我希望这能解决你的问题。不过,我必须说,在指示器运行时创建一个新的按钮项可能更有意义。检查以下各项:
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
let preservedButtonItem = self.navigationItem.rightBarButtonItem
let filterActivityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: activityIndicator)
return activityIndicator
}()
filterActivityIndicator.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
filterActivityIndicator.stopAnimating()
self.navigationItem.rightBarButtonItem = preservedButtonItem
}
}
编辑:多个按钮
对于多个按钮,情况应该非常相似。请尝试玩以下游戏:
guard let oldItems = navigationItem.rightBarButtonItems else {
return
}
var newItems = oldItems
// Find the index of item so we may ereplace it
guard let index = newItems.index(where: { $0 === filterMenuNavButton }) else {
return
}
let activityIndicator: UIActivityIndicatorView = {
let activity = UIActivityIndicatorView()
return activity
}()
newItems[index] = UIBarButtonItem(customView: activityIndicator)
navigationItem.rightBarButtonItems = newItems
DispatchQueue.main.asyncAfter(deadline: .now()+2.0) {
self.navigationItem.rightBarButtonItems = oldItems
}
关于ios - Swift-UIBarButtonItem.customView-重置为原始状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50936398/