我有一个我从故事板链接到的IBOutlet
@IBOutlet var creeLigueBouton: UIBarButtonItem!
如果条件成立,我想让它消失
if(condition == true)
{
// Make it disappear
}
你真的想隐藏/展示creeLigueBouton
吗?相反,启用/禁用UIBarButtonItems要容易得多。您可以使用以下几行来完成此操作:
if(condition == true) {
creeLigueBouton.enabled = false
} else {
creeLigueBouton.enabled = true
}
这段代码甚至可以用更短的方式重写:
creeLigueBouton.enabled = !creeLigueBouton.enabled
让我们在UIViewController子类中看到它:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var creeLigueBouton: UIBarButtonItem!
@IBAction func hide(sender: AnyObject) {
creeLigueBouton.enabled = !creeLigueBouton.enabled
}
}
如果你真的想显示/隐藏creeLigueBouton
,你可以使用以下代码:
import UIKit
class ViewController: UIViewController {
var condition: Bool = true
var creeLigueBouton: UIBarButtonItem! //Don't create an IBOutlet
@IBAction func hide(sender: AnyObject) {
if(condition == true) {
navigationItem.rightBarButtonItems = []
condition = false
} else {
navigationItem.rightBarButtonItems = [creeLigueBouton]
condition = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
creeLigueBouton = UIBarButtonItem(title: "Creer", style: UIBarButtonItemStyle.Plain, target: self, action: "creerButtonMethod")
navigationItem.rightBarButtonItems = [creeLigueBouton]
}
func creerButtonMethod() {
print("Bonjour")
}
}
试试这个。 (make newbackbutton全局变量)
override func viewDidLoad(){
let newBackButton = UIBarButtonItem()
newBackButton.title = " << Return to Gallery"
newBackButton.style = UIBarButtonItemStyle.Done
newBackButton.target = self
newBackButton.action = "backtoScoutDetail:"
self.navigationItem.rightBarButtonItem = newBackButton
}
override func viewWillAppear(animated: Bool) {
newBackButton.title = ""
self.navigationItem.rightBarButtonItem = newBackButton
}
我是这样做的:
navigationItem.setHidesBackButton(true, animated: true)
我有更多2个menuitems和删除/添加menuitem是一个开销。这段代码片段对我有用(使用Swift3)。
func showMenuItem(){
menuItemQuit.customView?.isHidden = false
menuItemQuit.plainView.isHidden = false
}
func hideMenuItem(){
menuItemQuit.customView?.isHidden = true
menuItemQuit.plainView.isHidden = true
}
现在回复已经很晚了但是找到了我的问题的答案我发现了这个话题。明确的答案对我没有帮助,但由于@ haiLong的回答,我设法解决了我的问题。我的解决方案适用于所有类型的条形按钮......我想。将其添加到ViewController并根据需要使用。
var tintColorsOfBarButtons = [UIBarButtonItem: UIColor]()
func hideUIBarButtonItem(button: UIBarButtonItem) {
if button.tintColor != UIColor.clear {
tintColorsOfBarButtons[button] = button.tintColor
button.tintColor = UIColor.clear
button.isEnabled = false
}
}
func showUIBarButtonItem(button: UIBarButtonItem) {
if tintColorsOfBarButtons[button] != nil {
button.tintColor = tintColorsOfBarButtons[button]
}
button.isEnabled = true
}
我希望它能节省一些时间给其他开发者:)
我在工具栏上遇到了同样的问题,我不得不隐藏它并显示它的最后一个按钮。所以我声明了一个var来保存UIBarButtonItem并将其从栏中删除或添加,具体取决于以下情况:
在类内部声明了var并链接了工具栏:
var buttonToHide : UIBarButtonItem?
@IBOutlet weak var toolbarOne: UIToolbar!
在viewDidLoad:
buttonToHide = toolbarOne.items![toolbarOne.items!.count - 1] as? UIBarButtonItem
在我的代码中我做了诀窍:
if situationOccurrsToHide {
toolbarOne.items!.removeLast()
}
要么
if situationOccursToShow
{
toolbarOne.items!.append(buttonToHide!)
}
您可以使用removeAtIndex或insert(buttonToHide,atIndex:xx)删除或重新插入特定位置的按钮。
您必须小心不要多次插入或移除按钮。
希望能帮助到你。
Try these:
self.navigationController?.navigationBar.backItem?.title = ""
navigationItem.backBarButtonItem?.title = ""
navigationItem.leftBarButtonItem?.title = ""
navigationItem.hidesBackButton = true
navigationItem.setLeftBarButtonItem(nil, animated: true)
navigationItem.setRightBarButtonItem(nil, animated: true)
使用启用的属性和tintColor
let barButtonItem:UIBarButtonItem? = nil
if isHidden{
barButtonItem?.enabled = false
barButtonItem?.tintColor = UIColor.clearColor()
}else{
barButtonItem?.enabled = true
barButtonItem?.tintColor = nil
}
// Nice answer haiLong, I think as an extension this is more convenient.
extension UIBarButtonItem {
var isHidden: Bool {
get {
return !isEnabled && tintColor == .clear
}
set {
tintColor = newValue ? .clear : nil
isEnabled = !newValue
}
}
}
编辑:删除强制解包和固定启用值。
对于Swift 3
if (your_condition) {
self.navigationItem.rightBarButtonItem = self.addAsset_btn
}
else {
// hide your button
self.navigationItem.rightBarButtonItem = nil
}
第一种方式:
只需将.title
设为""
即可
第二种方式:
只要你想显示/隐藏updateToolBar()
,就可以打电话给creeLigueBouton
。
func updateToolBar() {
var barItems: [UIBarButtonItem] = []
if condition != true {
// Make it appear
barItems.append(creeLigueBouton)
}
barItems.append(anotherButton)
myToolBar.setItems(barItems, animated: true)
myToolBar.setNeedsLayout()
}
继承我的解决方案:
隐藏:
self.creeLigueBouton.title = ""
self.creeLigueBouton.style = UIBarButtonItemStyle.Plain
self.creeLigueBouton.enabled = false
节目:
self.creeLigueBouton.title = "Original Button Text"
self.creeLigueBouton.style = UIBarButtonItemStyle.Bordered
self.creeLigueBouton.enabled = true
以下解决方案适合我。
var skipButton: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
skipButton.frame = CGRectMake(10.0, 0.0, 58.0, 32.0);
skipButton.setTitle("Skip", forState: UIControlState.Normal)
skipButton.setTitleColor(UIColor(red: 0.0, green: 122.0/255.0, blue: 255.0/255.0, alpha: 1.0), forState: UIControlState.Normal)
skipButton.addTarget(self, action: "rightButtonAction:", forControlEvents: UIControlEvents.TouchUpInside)
var skipButtonItem = UIBarButtonItem(customView: skipButton)
self.navigationItem.rightBarButtonItem = skipButtonItem;
if hideSkipButton == true {
self.navigationItem.rightBarButtonItem = nil
}
如果你有一组UIBarButtonItem
s要隐藏,例如只显示横向方向,隐藏或纵向,您可以使用标签和Swift数组的过滤器。让我们假设我们将@IBOutlet
链接到UIToolBar
:
@IBOutlet weak var toolbar: UIToolbar!
首先,我们在viewDidLoad
中保存工具栏的项目:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
toolbarItems = toolbar.items
}
将要在横向方向上显示的UIBarButtonItem的tag属性设置为1或您喜欢的任何内容。然后,覆盖func traitCollectionDidChange
override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
switch (traitCollection.horizontalSizeClass, traitCollection.verticalSizeClass) {
case (.Compact, .Regular): // iPhone Portrait
let items: [UIBarButtonItem]?
if view.frame.width > 320 { // iPhone 6 & 6S
items = toolbarItems?.filter({ $0.tag < 5 })
} else {
items = toolbarItems?.filter({ $0.tag < 4 })
}
bottomToolbar.setItems(items, animated: true)
case (_, .Compact): // iPhone Landscape
let items = toolbarItems?.filter({ $0.tag < 6 })
bottomToolbar.setItems(items, animated: true)
default: // iPad
break
}
}
在这个例子中,我将iPad的所有UIBarButtonItem
标签设置为6,iPhone Landscape设置为5,iPhone 6和6+设置为4。
您可以使用文本属性隐藏栏按钮:
barButton.enabled = false
barButton.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.clearColor()], forState: .Normal)
我还使用隐藏属性为UIBarButtonItem做了扩展:
extension UIBarButtonItem {
var titleTextAttributes: [NSObject : AnyObject]! {
set {
setTitleTextAttributes(newValue, forState: .Normal)
}
get {
return titleTextAttributesForState(.Normal)
}
}
private static var savedAttributesKey = "savedAttributes"
var savedAttributes: [NSObject : AnyObject]? {
set {
objc_setAssociatedObject(self, &UIBarButtonItem.savedAttributesKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
get {
return objc_getAssociatedObject(self, &UIBarButtonItem.savedAttributesKey) as? [NSObject : AnyObject]
}
}
var hidden: Bool {
set {
enabled = !newValue
if newValue {
savedAttributes = titleTextAttributes
// Set a clear text color
var attributes = titleTextAttributes
attributes[NSForegroundColorAttributeName] = UIColor.clearColor()
titleTextAttributes = attributes
}
else {
titleTextAttributes = savedAttributes
}
}
get {
return enabled
}
}
}