问题
I have researched this a lot and have used a couple of different approaches. This code is located in my ViewController.m:
1)1st Approach
UIImage *settingsimage = [UIImage imageNamed:@"gearicon"];
UIBarButtonItem *settingsBarButton =[[UIBarButtonItem alloc] initWithImage:settingsimage style:(UIBarButtonItemStylePlain) target:self action:@selector(switchpages)];
self.navigationItem.leftBarButtonItem = settingsBarButton;
this code executes without any errors, but no button appears!
2) 2nd Approach
UIButton *settingbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingbtn addTarget:self action:@selector(switchpages) forControlEvents:UIControlEventTouchUpInside];
[settingbtn setImage:[UIImage imageNamed:@"gearicon.png"] forState:UIControlStateNormal];
UIBarButtonItem *newBarItem = [[UIBarButtonItem alloc] initWithCustomView:settingbtn];
self.navigationItem.leftBarButtonItem = newBarItem;
Same as above, no errors, but no button. Please help where I've gone wrong. Thanks.
回答1:
You should try to the ImageView with image behind the button, it will do the trick
If you are not using any NavigationController and want to see how to add NavigationItem to the NavigationBar then you should make sure you add a NavigationBar on you xib and connect the IBOutlet in the Connection Inspector of it. here how your classes & code would look like...
in your .h file
@property(strong,nonatomic)UIImageView *navigationImageView;
@property(strong,nonatomic)UIButton *navigationButton;
@property (strong,nonatomic) IBOutlet UINavigationBar *navBar;
in .m file
@synthesize navigationButton;
@synthesize navigationImageView;
@synthesize navBar;
First create an ImageView and add the custom image inside if you are using the button with image
UIImage *backImage = [UIImage imageNamed:@"back.png"];
self.navigationImageView = [[UIImageView alloc] initWithImage:backImage];
now set the frame of the image view to the image
[self.navigationImageView setFrame: CGRectMake(5, 7, backImage.size.width, backImage.size.height)];
create the custom button and assign the image
self.navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.navigationButton setImage:backImage forState:UIControlStateNormal];
add image view to the navigation bar
[self.navBar addSubview:self.navigationImageView];
[self.navBar addSubview:self.navigationButton];
[self.navBar bringSubviewToFront:self.navigationButton];
create a UIBarButtonItem with the button as a custom view for custom method
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.navigationButton];
self.navigationItem.leftBarButtonItem = customBarItem;
[self.navigationButton addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
and finally your custom backMethod whatever you want to do in this... for testing you can show alert
-(void)backMethod{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Info" message:@"custom navigation" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
回答2:
So, here is my final solution. This puts a "gear" icon in the upper right hand side of a NavigationBar without a border:
.h
@property (strong, nonatomic) IBOutlet UINavigationBar *navBar;
@property (strong, nonatomic) IBOutlet UINavigationItem *settingsbtn;
@property(strong, nonatomic)UIButton *navigationButton;
.m
@synthesize navigationButton;
@synthesize settingsbtn;
@synthesize navBar;
ViewDidLoad
//custom settings button
navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton addTarget:self action:@selector(swithpages) forControlEvents:UIControlEventTouchUpInside];
[navigationButton setImage:[UIImage imageNamed:@"gearicon.png"]forState:UIControlStateNormal];
[navigationButton setFrame:CGRectMake(0, 0, 30, 35)];
UIBarButtonItem *settingsbutton = [[UIBarButtonItem alloc] initWithCustomView:navigationButton];
settingsbtn.rightBarButtonItem = settingsbutton;
The only thing I'm missing is having the button "glow" (like Apple's Remote App) upon pressed instead of color changing by default.
来源:https://stackoverflow.com/questions/15145279/putting-a-custom-button-on-a-nav-bar-in-ios6-not-working