iPad - 显示/隐藏UIBarButton

时间:2010-11-18 06:43:55

标签: iphone ipad uibarbuttonitem

有没有办法隐藏或显示UIBarButtonItem?

3 个答案:

答案 0 :(得分:3)

使用它来隐藏barbutton项目。

self.navigationItem.rightBarButtonItem = nil;

答案 1 :(得分: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 / controller methods

-(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
}

然后连接按钮并添加视图切换,这应该这样做......

假设你可以填写一些部分

答案 2 :(得分: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
}

}