2012-03-06 38 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參考取代自始終。