如何隐藏/显示导航栏中的右键

时间:2011-04-07 23:22:45

标签: ios objective-c uibarbuttonitem rightbarbuttonitem

我需要隐藏导航栏中的右键,然后在用户选择一些选项后取消隐藏它。

不幸的是,以下情况不起作用:

NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES;  // FOO CODE

有办法吗?

18 个答案:

答案 0 :(得分:73)

通过将引用设置为nil来隐藏按钮,但是如果您想稍后恢复它,则需要挂起它的副本以便重新分配。

UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;

//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];

就个人而言,在我的应用程序中,我将导航按钮设置为@properties,这样我就可以将其丢弃。随意重新创建它们,如下所示:

//mycontroller.h
UIBarButtonItem *rightNavButton;
@property (nonatomic, retain) UIBarButtonItem *rightNavButton;

//mycontroller.m
@synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
    if (!rightNavButton) {
        rightNavButton = [[UIBarButtonItem alloc] init];
        //configure the button here
    }
    return rightNavButton;
}

//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;

答案 1 :(得分:11)

对于Swift 3

public ActionResult BudgetNoteLineEntry(int accId)
    {
        BudgetEntryNotesVM NVM = new BudgetEntryNotesVM();

        NVM.AccountNumber = _budgetEntryRepository.GetAccountName(accId);
        NVM.AccountDescription = _budgetEntryRepository.GetAccountDescription(accId);

        NVM.BudgetNoteLineEntryList = _budgetEntryRepository.GetBudgetNoteLineEntry(accId);

        return PartialView("_ShowAccountBudgetLineEntries", NVM);
    }

答案 2 :(得分:9)

设置参考nil:

current_controller_in_navcontroller.navigationItem.rightBarButtonItem =  nil;

另外一定要在navController当前显示的控制器中调用它,而不是navController本身。

答案 3 :(得分:7)

显示:

[self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];

隐藏:

[self.navigationItem.rightBarButtonItem.customView setAlpha:0.0];

您甚至可以为其显示/隐藏

设置动画
[UIView animateWithDuration:0.2 animations:^{
        [self.navigationItem.rightBarButtonItem.customView setAlpha:1.0];

    }];

答案 4 :(得分:5)

这是Matt为Storyboards& amp;更新的解决方案弧。请记住,IBOutlets默认为__weak,因此您需要将其更改为strong,以免过早发布。

@interface MAGTableViewController () <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UIBarButtonItem *rightBarButton;

@end

@implementation MAGTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.navigationItem setRightBarButtonItem:nil];
}

- (IBAction)rightBarButtonItemTapped:(id)sender
{
    [self.view endEditing:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.navigationItem setRightBarButtonItem:self.rightBarButton];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self.navigationItem setRightBarButtonItem:nil];
}

@end

答案 5 :(得分:4)

SWIFT 2.2

在swift 2.2中,self.navigationItem不起作用。而是创建NavigationItem的出口(我将其命名为&#34; nav&#34;)并使用它。

以下建议对我使用Xcode 7.3和swift 2.2

也不起作用
@IBOutlet weak var settingItem: UIBarButtonItem!
@IBOutlet weak var logoItem: UIBarButtonItem!

var sav_settingItem: UIBarButtonItem = UIBarButtonItem()
var sav_logoItem: UIBarButtonItem = UIBarButtonItem()

所以我上面用@Matt J的想法如下(我左边有2个项目):

  1. 为导航栏中的项目创建出口,并为变量存储

    sav_settingItem = nav.leftBarButtonItems![0]
    sav_logoItem = nav.leftBarButtonItems![1]
    
  2. 将项目保存在viewDidLoad()

    nav.leftBarButtonItem = nil
    nav.leftBarButtonItem = nil
    
  3. 要HIDE将它们设置为nil

    if (nav.leftBarButtonItem == nil) {
        nav.leftBarButtonItem = sav_settingItem
        nav.leftBarButtonItems?.append(sav_logoItem)
        return
    }
    
  4. 要显示它们

    def ToUnixPath(path):
    rv = SplitPath(path)
    rv[:] = [item for item in rv if item != '/']
         rv = '\\'.join(rv)
         return r"\\?"+"\\"+rv
    

答案 6 :(得分:3)

对于这个答案来说,学分必须转到 learner ,答案来自这个问题:

hide and show left navigation bar button on demand in iOS-7

这是答案,更简单。

//hide and reveal bar buttons
-(void) hideAndDisableLeftNavigationItem
{
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor clearColor]];
    [self.navigationItem.leftBarButtonItem setEnabled:NO];
}

-(void) showAndEnableLeftNavigationItem
{
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blueColor]];
    [self.navigationItem.leftBarButtonItem setEnabled:YES];
}

然后你只需像(IBAction)那样引用你需要它的方法:

[self hideAndDisableLeftNavigationItem];//[self showAndEnableLeftNavigationItem]; to show again

我尝试了所有其他方法,但都没有工作,甚至将我的按钮引用为@property (...) UIBarButtonItem....,直到我找到它之前没有任何工作。

答案 7 :(得分:3)

显示:

Application.Handler.ModuleName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

隐藏:

[self.navigationItem.rightBarButtonItem.customView setHidden:NO];

答案 8 :(得分:2)

Swift 2

  

招!

隐藏:

if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
        btn.enabled = false
        btn.title = ""
}

显示:

if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
        btn.enabled = true
        btn.title = "ButtonName"
}

答案 9 :(得分:1)

我的解决方案:

self.navigationItem.rightBarButtonItem.customView.hidden=NO;

答案 10 :(得分:1)

如果右侧只有一个条形按钮项,则可以使用它,

  

self.navigationItem.rightBarButtonItem = nil;

假设您在右侧有多个条形按钮,例如,假设您在导航项的右侧有两个条形按钮项目(搜索按钮和过滤器按钮)。现在,右栏按钮项是

  

self.navigationItem.rightBarButtonItems = [searchItem,filterItem]

,您只需隐藏搜索按钮,就可以使用

  

self.navigationItem.rightBarButtonItems = [filterItem]

现在发生了什么,您可以将导航按钮中的搜索按钮完全隐藏起来,而过滤器项将代替搜索项

答案 11 :(得分:1)

swift 4 中,我有一个技巧来显示/隐藏右或左按钮:

步骤1::在视图控制器中创建IBOutlet按钮:

@IBOutlet var navigationItemButton: UIBarButtonItem!

步骤2:创建“隐藏”按钮功能:

func hideNavigationButton() {
    navigationItemButton.isEnabled = false
    navigationItemButton.tintColor = UIColor.clear
}

第3步::创建显示按钮功能:

func showNavigationButton() {
    navigationItemButton.isEnabled = true
    navigationItemButton.tintColor = UIColor.white
}

第4步:只需调用所需的函数,使用hideNavigationButton()隐藏,然后使用showNavigationButton()显示按钮。

致谢!

答案 12 :(得分:0)

用于快速5隐藏rightBarButtonItem

self.navigationItem.rightBarButtonItem?.customView?.isHidden = true

答案 13 :(得分:0)

您可以使用以下代码:

<form class='main'>

  <input id='act1' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='1'>Question</legend>
    <label class='tgr' for='act1'>Answers</label>
    <dl class='cat'>
      <dt>Category 1A</dt>
      <dd>Topic 1A1</dd>
      <dd>Topic 1A2</dd>
      <dt>Category 1B</dt>
      <dd>Topic 1B1
        <a href='#t5c2' class='lnx' title='Topic 5C2'>Also see</a>
      </dd>
      <dd>Topic 1B2</dd>
    </dl>
  </fieldset>

  <input id='act2' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='2'>Question</legend>
    <label class='tgr' for='act2'>Answers</label>
    <dl class='cat'>
      <dt>Category 2A</dt>
      <dd>Topic 2A1</dd>
      <dd>Topic 2A2</dd>
      <dt>Category 2B</dt>
      <dd>Topic 2B1</dd>
    </dl>
  </fieldset>

  <input id='act3' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='3'>Question</legend>
    <label class='tgr' for='act3'>Answers</label>
    <dl class='cat'>
      <dt>Category 3A</dt>
      <dd>Topic 3A1</dd>
      <dt>Category 3B</dt>
      <dd>Topic 3B1</dd>
      <dd>Topic 3B2</dd>
      <dd>Topic 3B3</dd>
    </dl>
  </fieldset>

  <input id='act4' class="act" type="checkbox" hidden>
  <fieldset class='set'>
    <legend class='que' data-idx='4'>Question</legend>
    <label class='tgr' for='act4'>Answers</label>
    <dl class='cat'>
      <dt>Category 4A</dt>
      <dd>Topic 4A1</dd>
      <dd>Topic 4A2</dd>
      <dd>Topic 4A3</dd>
    </dl>
  </fieldset>

  <input id='act5' class="act" type="checkbox" hidden>
  <fieldset id='t5c2' class='set'>
    <legend class='que' data-idx='5'>Question</legend>
    <label class='tgr' for='act5'>Answers</label>
    <a href='#act5' class='tgr'>Answers</a>
    <dl class='cat'>
      <dt>Category 5A</dt>
      <dd>Topic 5A1</dd>
      <dd>Topic 5A2</dd>
      <dt>Category 5B</dt>
      <dd>Topic 5B1</dd>
      <dd>Topic 5B2</dd>
      <dt>Category 5C</dt>
      <dd>Topic 5C1</dd>
      <dd class='top'>Topic 5C2</dd>
    </dl>
  </fieldset>

</form>

答案 14 :(得分:0)

首先将标题设置为空,然后再旋转一次。

答案 15 :(得分:0)

  1. 假设您可以将特定的条形按钮引用为变量xxxButton
  2. (请打开助理编辑器,按Ctrl +拖动xxx按钮到YourViewController类作为插座“xxxButton”)。

    或者您可以使用let xxxButton = navigationBar.buttons[1]

    之类的内容
    1. 隐藏 xxxButton.customView = UIView() 要么 navigationItem.rightBarButtonItems?.remove(at: (navigationItem.rightBarButtonItems?.index(of:xxxButton)!)!)

    2. 显示 xxxButton.customView = nil 要么 navigationItem.rightBarButtonItems?.insert(newElement: xxxButton, at:SOME_INDEX)

    3. 希望有所帮助。

答案 16 :(得分:0)

隐藏:

if let topItem = self.navigationController?.navigationBar.topItem {
    topItem.rightBarButtonItem = nil
}

答案 17 :(得分:0)

显示:

//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];

隐藏:

//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];