我有一个导航栏按钮项,该项在数据查询期间添加了自定义活动指示器视图。查询完成后,我想将按钮设置回其原始状态。这是我当前的代码:
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,这没有任何作用。我试过只是重新设置条形按钮项目的背景图像,但没有执行任何操作。有任何想法吗?谢谢!
编辑:
我尝试操作的UIBarButtonItem在右侧,但右侧有两个条形按钮项。 filterMenuNavButton
是我要操作的特定按钮的IBOutlet。
答案 0 :(得分:1)
有时无效是一个问题。看来您需要再次将按钮设置到导航栏。它是如此聪明,您首先需要将其设置为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
}
答案 1 :(得分:0)
使用自定义视图作为条形按钮项目时,我没有遇到任何问题:
fileprivate var underlyingButton: UIButton?
...
let barButton = UIButton(type: .custom)
barButton.addTarget(self, action: #selector(stuffs), for: .touchUpInside)
barButton.setImage(image, for: UIControlState())
let rightItem = UIBarButtonItem(customView: barButton)
self.filterMenuNavButton = rightItem
self.underlyingButton = barButton
然后我可以简单地打电话给我
guard let button = underlyingButton else {
return
}
button.setImage(someOtherImage, for: UIControlState())