2012-03-06 71 views
2

我已经阅读了关于StackOverflow上的这个主题的几个类似的问题,但是它们都不适用于我的案例。显示/隐藏位于UIToolbar中间的UIBarButtonItem

我在我的应用程序上有一个UIToolbar。工具栏具有如下的布局。

(Button #1) (Button #2) (Button #3) <--- Flex Controller ---> (Button #4) 

什么是显示/隐藏Button #3的UIBarButtonItem,因为它是在UIToolbar中间的最有效的方法是什么?

的后续

我知道setItems:Animated方法,但是这似乎是因为我所有的按钮都通过IB有线和他们相关的IBActions它可能是有问题的。

这是否意味着我需要找到一种方法在隐藏时保留我的Button#3(因为我正在使用ARC?)我正在寻找最佳/有效的方法来添加和删除这些对象。如果我每次必须显示/隐藏我的#3按钮时都必须重新创建此列表,那就是我要做的。

它看起来效率很低。在UIToolBar.h

+0

可能你已经尝试过了,但是不能只使用按钮的'hidden'属性吗? – Novarg 2012-03-06 16:38:51

+0

@Novarg:该属性在UIBarButtonItem上不可用,因为它不是从UIButton继承的。 – RLH 2012-03-06 16:41:01

+0

将按钮连接为IBOutlets,然后在调整工具栏的项目时可以参考它们。我已经写了一个答案,描述了我在使用这种方法简化问题时使用的方法。 – Chad 2012-03-06 16:59:48

回答

3

我会建议通过以下方式建立一个辅助功能:

- (void) setToolbarItems:(BOOL) button3Shown { 
    NSMutableArray *items = [NSMutableArray arrayWithCapacity:5]; 
    [items addObject:button1]; 
    [items addObject:button2]; 
    if(button3Shown) { 
     [items addObject:button3]; 
    } 
    [items addObject:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]]; 
    [items addObject:button4]; 
    [self.toolbar setItems:[items copy] animated:YES]; 
} 

这于是具有预定可每当你需要更新它,然后你可以轻松地添加/删除按钮的利益。为了实现这一点,您需要将实现文件中的按钮作为IBOutlet使用,并且可以轻松引用它们以重建工具栏。

+0

只是一个说明,这是一个很好的解决方案,但使用'initWithCustomView:'方法创建的'UIBarButtonItem'也需要设置它们的框架。 – oflannabhra 2015-10-19 13:05:14

4

来看,有以下方法:

- (void)setItems:(NSArray *)items animated:(BOOL)animated; // will fade in or out or reorder and adjust spacing 

你应该只能够做一些事情,如:

NSMutableArray *items = [toolBar.items mutableCopy]; 
[items removeObjectAtIndex:2]; 
[toolBar setItems:items animated:YES]; 

看看是否能运作的,你正在寻找

方式
+0

+1打我一分:) – Jim 2012-03-06 16:40:53

+1

现在,我应该如何添加回被删除的项目,一旦它应该显示?仅供参考,该按钮通过IB连接并具有关联的IBAction。 – RLH 2012-03-06 16:49:57

+0

您必须将'UIBarButtonItem'存储在一个实例变量中,并通过类似的过程将其添加回来。来自IB不应该影响任何东西。 – 2012-03-06 16:58:18

0

在运行时创建不同的UIBarButtonItems数组并在UIToolbar中添加/删除UBSoolbar可能会增加执行的复杂度。

我已经使用不同的方法做到了。以下是实现它的片段。

第1步:创建一个IBOutlet或创建UIToolbar实例。

UIToolbar *toolBar = [UIToolbar alloc] init]; 

,如果你想设置所需的样式。

第2步:创建UIButton类型实例。

UIButton *button1, *button2; 

第3步:将图像(如果需要)分配给按钮。

button1 = [UIButton alloc] initWithFrame:CGRectMake(0.f,0.f,30.f,30.f); 
    [button1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal]; 

    button2 = [UIButton alloc] initWithFrame:CGRectMake(0.f,0.f,30.f,30.f); 
    [button2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal]; 

第4步:使用上述按钮创建UIBarButtonItem实例作为自定义视图。

UIBarButtonItem *toolbarbuttonItem1 = [UIBarButtonItem alloc] initWithCustomView:button1]; 
    UIBarButtonItem *toolbarbuttonItem2 = [UIBarButtonItem alloc] initWithCustomView:button1]; 

第5步:将条形按钮项目分配给工具栏。

toolBar.items = [NSArray arrayWithObjects:toolbarbuttonItem1,toolbarbuttonItem2,nil]; 

第6步:将工具栏添加到视图。

第7步:在VIEW中创建一个UIButton IBOutlet并分配一个单击事件。

在click事件中调用以下帮助方法以切换隐藏。

- (void) toggleToolbarButtons { 
      button1.hidden = !button1.hidden; 
      button2.hidden = !button2.hidden; 
    } 

希望帮助!!

0

通过对工具栏项目item1,item2和item3的现有引用,最直接的方法是删除所有项目,然后按照需要的顺序重新添加每个项目。此代码是从UIToolbar子类的角度编写的:

let flexSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) 
let fixedSpace: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil) 

self.items?.removeAll() 
self.items?.append(flexSpace) 
self.items?.append(item1) 
self.items?.append(fixedSpace) 
self.items?.append(item3) 
self.items?.append(fixedSpace) 
self.items?.append(item2) 

这些项目引用可以来自IBOutlets。将这些引用更改为强而不是IB插入的默认弱引用,这样即使UIToolbar已被删除,UIToolbar也会保持对这些项的引用,从而维护相关的IBAction函数关系。

要从UIToolbar从UIToolbar子类中删除特定的项目,而不必重新设置所有项目:

if let index = self.items?.index(of: buttonToRemove) { 
    self.items?.remove(at: index) 
} 

如果没有一个UIToolbar子类中,只需在UIToolbar参考取代自始终。