2010-11-18 59 views
1

有沒有辦法隱藏或顯示UIBarButtonItem?iPad - 顯示/隱藏UIBarButton

+0

我們可以通過它的屬性設置爲無,每當我們要顯示它,然後我們就可以在狀態只寫這樣我們就可以顯示按鈕隱藏導航項目。 – AppAspect 2010-12-09 06:49:26

回答

3

使用此來隱藏barbutton項目。

self.navigationItem.rightBarButtonItem = nil;

2

實際上,UIBarButtonItem不會響應.hidden。它不是UIButton的子類。

下面是如何切換三個按鈕左側按鈕中間按鈕和右側按鈕的快速示例。

//所以,在適當的位置

IBOutlet UIToolbar *bottomToolbar; 
IBOutlet UIBarButtonItem *leftButton; 
IBOutlet UIBarButtonItem *flexerSpace; 
IBOutlet UIBarButtonItem *middleButton; 
IBOutlet UIBarButtonItem *rightButton; 

//那麼這在適當的位置

@property (retain, nonatomic) IBOutlet UIToolbar *bottomToolbar; 
@property (retain, nonatomic) IBOutlet UIBarButtonItem *leftButton; 
@property (retain, nonatomic) IBOutlet UIBarButtonItem *flexerSpace; 
@property (retain, nonatomic) IBOutlet UIBarButtonItem *middleButton; 
@property (retain, nonatomic) IBOutlet UIBarButtonItem *rightButton; 

//然後在適當的位置

@synthesize bottomToolbar; 
@synthesize leftButton; 
@synthesize flexerSpace; 
@synthesize middleButton; 
@synthesize rightButton; 

再重裝你的班級IB或Xcode4 IB在你的筆尖

//然後在

view_switcher /控制器方法

-(IBAction)switchToLeft:(id)sender { 

NSArray *newButtonArray = [NSArray arrayWithObjects: middleButton, flexerSpace, rightButton, nil]; 

[bottomToolbar setItems:newButtonArray animated:NO]; //YES or NO either way it 
} 

-(IBAction)switchToMiddle:(id)sender { 

NSArray *newButtonArray = [NSArray arrayWithObjects: leftButton, flexerSpace, rightButton, nil]; 

[bottomToolbar setItems:newButtonArray animated:NO]; //YES or NO either way it 
} 

-(IBAction)switchToRight:(id)sender { 

NSArray *newButtonArray = [NSArray arrayWithObjects: leftButton, flexerSpace, middleButton, nil]; 

[bottomToolbar setItems:newButtonArray animated:NO]; //YES or NO either way it 
} 

然後連接按鈕,並添加您的視圖切換,這應該這樣做...

假設你可以在一些地方

填寫
1

這是我的UIBarButtonItem子類來公開一個setHidden方法。每當我在酒吧上有一個「可選」按鈕時,我都會使用它。 它適用於文本或圖像按鈕。

class AltoUIBarButtonItem: UIBarButtonItem { 

var originalImage : UIImage? 
var originalTitle : String? 

override func awakeFromNib() { 

    originalTitle = self.title 
    originalImage = self.image 
} 

func setHidden(_ hidden: Bool) 
{ 
    if hidden 
    { 
     if originalImage != nil { self.image = nil} 
     if originalTitle != nil { self.title = ""} 
     self.isEnabled = false 
    } 
    else 
    { 
     if originalImage != nil { self.image = originalImage} 
     if originalTitle != nil { self.title = originalTitle} 
     self.isEnabled = true 
    } 
} 

func setOriginalTitle(_ originalTitle: String?) 
{ 
    self.originalTitle = originalTitle 
} 

}