Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用swift IOS使一个uibarbuttonite消失_Swift_Conditional Statements_Uibarbuttonitem_Iboutlet - Fatal编程技术网

使用swift IOS使一个uibarbuttonite消失

使用swift IOS使一个uibarbuttonite消失,swift,conditional-statements,uibarbuttonitem,iboutlet,Swift,Conditional Statements,Uibarbuttonitem,Iboutlet,我有一个从故事板链接到的IBOutlet @IBOutlet var creeLigueBouton: UIBarButtonItem! 如果条件成立,我想让它消失 if(condition == true) { // Make it disappear } 是否确实要隐藏/显示克里利格布顿?相反,启用/禁用UIBarButtonims要容易得多。您可以使用几行代码来完成此操作: if(condition == true) { creeLigueBouton.enabled

我有一个从故事板链接到的IBOutlet

@IBOutlet var creeLigueBouton: UIBarButtonItem!
如果条件成立,我想让它消失

if(condition == true)
{
    // Make it disappear
}

是否确实要隐藏/显示克里利格布顿?相反,启用/禁用UIBarButtonims要容易得多。您可以使用几行代码来完成此操作:

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")
    }

}

下面的解决方案适合我

        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
    } 

我的工具栏也有同样的问题,我不得不隐藏并显示它的最后一个按钮。因此,我声明了一个var来保存uibarbuttonite,并将其从栏中移除或添加,具体取决于以下情况:

在类内部声明了var并链接了工具栏:

var buttonToHide : UIBarButtonItem?

@IBOutlet weak var toolbarOne: UIToolbar!
在viewDidLoad中:

buttonToHide = toolbarOne.items![toolbarOne.items!.count - 1] as? UIBarButtonItem
在我的代码中,我做了一个技巧:

if situationOccurrsToHide {
   toolbarOne.items!.removeLast()
}

您可以使用removeAtIndex或insert(buttonToHide,atIndex:xx)在特定位置移除或重新插入按钮

必须小心不要多次插入或移除按钮


希望有帮助。

第一种方法:

只需将
.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()
}
以下是我的解决方案:

隐藏:

展示:


如果您有一组要隐藏的
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
    }
  }
override func traitCollectionDidChange(以前的traitcollection:UITraitCollection?){
super.traitCollectionDidChange(以前的traitCollection)
交换机(traitCollection.horizontalSizeClass、traitCollection.verticalSizeClass){
大小写(.Compact,.Regular)://iPhone肖像
让项目:[uibarbuttonite]?
如果view.frame.width>320{//iPhone 6和6S
items=toolbarItems?.filter({$0.tag<5})
}否则{
items=toolbarItems?.filter({$0.tag<4})
}
bottomToolbar.setItems(项目,动画:true)
大小写(_,.Compact)://iPhone横向
let items=toolbarItems?.filter({$0.tag<6})
bottomToolbar.setItems(项目,动画:true)
默认值://iPad
打破
}
}

在本例中,我将iPad的all
uibarbuttoneim
标记仅设为6,iPhone横向设为5,iphone6&6+设为4

可以使用文本属性隐藏条形按钮:

barButton.enabled = false
barButton.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.clearColor()], forState: .Normal)
此外,我还使用一个隐藏属性对uibarbuttonite进行了扩展:

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
        }
    }
}
试试这个。(使newbackbutton成为全局变量)

重写func viewDidLoad(){

我是这样做的:

navigationItem.setHidesBackButton(true, animated: true)

使用已启用的属性和“着色颜色”

    let barButtonItem:UIBarButtonItem? = nil

    if isHidden{
        barButtonItem?.enabled      = false
        barButtonItem?.tintColor    = UIColor.clearColor()
    }else{
        barButtonItem?.enabled      = true
        barButtonItem?.tintColor    = nil
    }

编辑:删除强制展开和固定启用值。

用于Swift 3

if (your_condition) {
  self.navigationItem.rightBarButtonItem = self.addAsset_btn
 }
else {
  // hide your button
  self.navigationItem.rightBarButtonItem = nil
 }

我有两个以上的menuitem,删除/添加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
    }

我希望它能为其他开发人员节省一些时间:)

非常感谢这正是我所寻找的,并且我正在寻找一个禁用的按钮。有可能修改文本颜色吗?寻找了很长时间,这是到目前为止的完美答案。@JSA986是的……这是真的(第二种方法可以工作,即使条形按钮项没有标题)第二种方法不起作用,除非你创建一个强大的outlet引用或实例化你的按钮程序MaticallyTanks,伙计!为我工作。太棒了!工作得很好。这很好,但如果你的UIBarButtonim包含自定义视图,它就不起作用,如果它包含自定义视图,你可以直接在该视图上调用hidden。这正是我想要的,谢谢你发布你的自定义视图解决方案
navigationItem.setHidesBackButton(true, animated: true)
    let barButtonItem:UIBarButtonItem? = nil

    if isHidden{
        barButtonItem?.enabled      = false
        barButtonItem?.tintColor    = UIColor.clearColor()
    }else{
        barButtonItem?.enabled      = true
        barButtonItem?.tintColor    = nil
    }
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)
// 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
        }
    }
}
if (your_condition) {
  self.navigationItem.rightBarButtonItem = self.addAsset_btn
 }
else {
  // hide your button
  self.navigationItem.rightBarButtonItem = nil
 }
func showMenuItem(){

    menuItemQuit.customView?.isHidden = false
    menuItemQuit.plainView.isHidden = false
}

func hideMenuItem(){

    menuItemQuit.customView?.isHidden = true
    menuItemQuit.plainView.isHidden = true
}
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
    }