iPad - Show/Hide UIBarButton
Is there a way开发者_如何学C to hide or show a UIBarButtonItem?
USe this to hide barbutton item.
self.navigationItem.rightBarButtonItem = nil;
Actually, UIBarButtonItem does not respond to .hidden. It is not a subclass of UIButton.
Here is a quick example of how you can switch three buttons a left button a middle button and a right button.
//So, in the proper place
IBOutlet UIToolbar *bottomToolbar;
IBOutlet UIBarButtonItem *leftButton;
IBOutlet UIBarButtonItem *flexerSpace;
IBOutlet UIBarButtonItem *middleButton;
IBOutlet UIBarButtonItem *rightButton;
//Then this in the proper place
@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;
//Then in the proper place
@synthesize bottomToolbar;
@synthesize leftButton;
@synthesize flexerSpace;
@synthesize middleButton;
@synthesize rightButton;
then reload your class in IB or Xcode4 IB in your nib
// then in your
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
}
then connect the buttons and add your view switching, this should do it ...
Assuming you can fill in some parts
This is my UIBarButtonItem subclass to expose a setHidden method. I use it whenever I have an "optional" button on my bars. It works for textual or image buttons.
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
}
}
精彩评论