ios 如何为某些用户隐藏条形按钮项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27887218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:18:41  来源:igfitidea点击:

How to hide a bar button item for certain users

iosswiftuibarbuttonitem

提问by grabury

I have a settings bar button item (set as left bar button item). I only want to display it if the user is logged in.

我有一个设置栏按钮项目(设置为左栏按钮项目)。我只想在用户登录时显示它。

I thought I could use the following for anonymous users

我以为我可以对匿名用户使用以下内容

navigationItem.leftBarButtonItem = nil

But then how would I show it as soon as they logged in?

但是,一旦他们登录,我将如何显示它?

回答by rakeshbs

You can store a copy of the leftBarButtonItem in a strongproperty and update it after the users log in.

您可以在strong属性中存储 leftBarButtonItem 的副本,并在用户登录后更新它。

var leftBarButtonItem : UIBarButtonItem!

Inside viewDidLoad:

内部viewDidLoad

self.leftBarButtonItem = UIBarButtonItem(title: "test", style:         UIBarButtonItem.Style.Plain, target: nil, action: nil)

In logic:

在逻辑上:

if loggedIn
{
    self.navigationItem.leftBarButtonItem = self.leftBarButtonItem
}
else
{
    self.navigationItem.leftBarButtonItem = nil
}

回答by jeff ayan

Best Way is just custom your Bar buttom with image. Set barbuttom.image = nilto Hide again assign the image to show. And dont forget to make the barbutton isEnabled as false.

最好的方法只是用图像自定义您的酒吧按​​钮。barbuttom.image = nil再次设置为隐藏指定要显示的图像。并且不要忘记将 barbutton isEnabled 设置为 false。

回答by Manikanta

I have more that 2 menuitems and remove/add menuitem is an overhead. This code snippet worked for me.

我有超过 2 个菜单项,删除/添加菜单项是开销。这个代码片段对我有用。

func showMenuItem(){

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

func hideMenuItem(){

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

回答by Kiran jadhav

if you want to hide/show UIBarButtonItem : For Swift 3

如果你想隐藏/显示 UIBarButtonItem :对于 Swift 3

Used below simple code :

使用下面的简单代码:

Declaration :

宣言 :

var doneButton = UIBarButtonItem()

In ViewDidLoad() or ViewWillAppear() or where you want to hide it : [hide bar button]

在 ViewDidLoad() 或 ViewWillAppear() 或您想隐藏它的地方:[隐藏栏按钮]

self.navigationItem.rightBarButtonItem = nil

where you want to show bar button : [use anywhere in your code]

您想要显示条形按钮的位置:[在代码中的任何地方使用]

self.navigationItem.rightBarButtonItem = self.doneButton
        doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.dismissPicker))

回答by zeeshan

Swift 5.x

斯威夫特 5.x

I faced the same dilemma and unfortunately no solution worked for me. Adding and removing buttons and related segues is unnecessarily too much of code when it includes multiple buttons on multiple screens. I have taken this approach for one or two buttons in the past and it becomes pretty ugly pretty fast.

我面临着同样的困境,不幸的是没有解决方案对我有用。当它在多个屏幕上包含多个按钮时,添加和删除按钮和相关的 segue 是不必要的代码太多。过去我对一两个按钮采取了这种方法,它很快变得非常丑陋。

The code menuItemQuit.customView?.isHidden = falsedoesn't seem to work on iOS 13 and above either, otherwise it would have made life so much easier.

该代码menuItemQuit.customView?.isHidden = false似乎也不适用于 iOS 13 及更高版本,否则它会让生活变得更加轻松。

My approach was to simply disable the bar button and change its tint to the navigation colour's tint.

我的方法是简单地禁用栏按钮并将其色调更改为导航颜色的色调。

In my app What.To.EatI display bar buttons based on user's login status. Every element of the app is themed so that I could control all the colors based on various factors.

在我的应用程序What.To.Eat 中,我根据用户的登录状态显示栏按钮。应用程序的每个元素都有主题,因此我可以根据各种因素控制所有颜色。

The navigation bar's color is named commonButtonColorand the bar buttons tint color is named commonButtonColor.

导航栏的颜色被命名commonButtonColor,栏按钮的颜色被命名为commonButtonColor

When I have to hide a bar button, I simply do the following:

当我必须隐藏一个栏按钮时,我只需执行以下操作:

let nav = self.navigationController?.navigationBar
nav?.tintColor = Theme.shared.titleText
nav?.barTintColor = Theme.shared.headerBg

if person.loggedIn {
    mealPrefsBarButton.tintColor = Theme.shared.commonButtonColor
    mealPrefsButton.isEnabled = true
} else {
    mealPrefsBarButton.tintColor = Theme.shared.headerBg
    mealPrefsButton.isEnabled = false
}

Where theme colors are defined in a separate file like this:

主题颜色在一个单独的文件中定义,如下所示:

static var headerBg: UIColor {
    return UIColor(red: 0.965, green: 0.969, blue: 0.973, alpha: 1.00)
}

The above is a simplified version of what I do in the app to make it clear what I am doing. I hope it would help someone trying to achieve the same. It is simple solution and works just perfectly with a few lines of code.

以上是我在应用程序中所做的简化版本,以明确我在做什么。我希望它会帮助试图实现相同目标的人。这是一个简单的解决方案,只需几行代码即可完美运行。

As an example from the app, this is how two buttons appear and disappear based on whether the My Recipesbutton is selected or not:

作为应用程序的示例,以下是根据My Recipes按钮是否被选中来显示和消失两个按钮的方式:

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by dobiho

I have a same problem and solved. I have a bar button item with image

我有同样的问题并解决了。我有一个带有图像的条形按钮项目

barbtnClose.isEnabled = false
barbtnClose.image = nil

barbtnClose.customView?.isHidden = true // do not work in iOS 13