Swift - UIBarButtonItem.customView - 重置为原始状态
Swift - UIBarButtonItem.customView - reset to original state
我有一个导航栏按钮项目,它在数据查询期间添加了自定义 activity 指示器视图。查询完成后,我想将按钮设置回其原始状态。这是我当前的代码:
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。
有时失效是个问题。看来您需要再次将按钮设置为导航栏。它非常聪明,您首先需要将其设置为 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
中。
最后一部分可能是您正在寻找的,您需要:
- 将自定义视图设置为清除
- 添加图像或更改其他按钮属性
- 清除按钮项(设置为 nil)
- 将按钮项目设置回所需的项目
希望这能解决您的问题。我必须说,在指示器运行时创建一个新的按钮项可能更有意义。检查以下内容:
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: { [=12=] === 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
}
使用自定义视图作为栏按钮项时,我没有遇到任何问题:
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())
我有一个导航栏按钮项目,它在数据查询期间添加了自定义 activity 指示器视图。查询完成后,我想将按钮设置回其原始状态。这是我当前的代码:
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。
有时失效是个问题。看来您需要再次将按钮设置为导航栏。它非常聪明,您首先需要将其设置为 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
中。
最后一部分可能是您正在寻找的,您需要:
- 将自定义视图设置为清除
- 添加图像或更改其他按钮属性
- 清除按钮项(设置为 nil)
- 将按钮项目设置回所需的项目
希望这能解决您的问题。我必须说,在指示器运行时创建一个新的按钮项可能更有意义。检查以下内容:
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: { [=12=] === 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
}
使用自定义视图作为栏按钮项时,我没有遇到任何问题:
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())