我想在导航栏中更改后退按钮的后退按钮外观。为此,我有这段代码。
UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.backgroundColor = [UIColor colorWithPatternImage:backButtonImage];
backbutton.titleLabel.text = @"back";
backbutton.titleLabel.textColor= [UIColor colorWithRed:50/255.0
green:158/255.0
blue:218/255.0
alpha:1.0];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
但我仍然得到标准后退按钮。有什么帮助吗?
答案 0 :(得分:1)
在将viewController推送到堆栈之前,先将代码设置为自定义后退按钮。并设置BackBarButtonItem,而不是leftBarButtonItem。
UIImage *image = [UIImage imageNamed:@"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[backbutton setImage:image forState:UIControlStateNormal];
backbutton.titleLabel.textColor = [UIColor blueColor];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[[self navigationItem] setBackBarButtonItem: back];
[self.navigationController pushViewController:yourViewControllerObject animated:YES];
答案 1 :(得分:0)
使用
self.navigationItem.leftBarButtonItem = back;
而不是
self.navigationController.navigationItem.leftBarButtonItem = back;
答案 2 :(得分:0)
根据您的要求使用波纹管代码..
- (void)viewDidLoad
{
///write your code
UIImage *backButtonImage = [UIImage imageNamed:@"backbutton.png"];
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//[backbutton setImage:backButtonImage forState:UIControlStateNormal];//this line for set only image on button
[backbutton setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[backbutton setTitle:@"Back" forState:UIControlStateNormal];
backbutton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
UIBarButtonItem * back = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[backbutton addTarget:self action:@selector(back_Clicked) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = back;
}
当后退按钮单击方法调用时,您将转到上一个viewcontroller
-(void)back_Clicked{
[self.navigationController popViewControllerAnimated:YES];
}
答案 3 :(得分:0)
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[self buttonWithImage:@"backButton" heighlightImageName:@"backButton" buttonFrame:CGRectMake(2, 1, 30, 30) selectorName:@selector(buttonBackClicked:) target:self]]];
- (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[tempButton setFrame:buttonFrame];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
return tempButton;
}
- (UIImage*)getImageFromResource:(NSString*)imageName
{
return [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:@"png"]];
}
答案 4 :(得分:0)
IButton *favButton = [[UIButton alloc] init];
[favButton setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
[favButton addTarget:self action:@selector(favouriteButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithCustomView:favButton];
self.navigationItem.rightBarButtonItem = button;
[button release];
[favButton release];