Adding custom back button on navigation bar - ios

I am trying to add custom back button on navigation bar but it is not working below is my code
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
Please tell what's wrong with this code

Try this.
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:aBarButtonItem];
}

Here is the code that i have used to create custom backbutton
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the custom back button
//add the resizable image
UIImage *buttonImage = [[UIImage imageNamed:#"backbtn.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 5)];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
MSLabel *lbl = [[MSLabel alloc] initWithFrame:CGRectMake(12, 4, 65, 28)];
lbl.text = #"Settings";
lbl.backgroundColor = [UIColor clearColor];
//lbl.font = [UIFont fontWithName:#"Helvetica" size:12.0];
lbl.numberOfLines = 0;
lbl.lineHeight = 12;
lbl.verticalAlignment = MSLabelVerticalAlignmentMiddle;
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
[button addSubview:lbl];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 70, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
}
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
This code will resize your image so it can fit the text.
You can also put this button in any method.

You didn't add action to your button. So you need to add action to your button. For more info see in the below code..
-(void)addLeftButton
{
UIImage *buttonImage = [UIImage imageNamed:#"btn_back.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(backBtnPressed) forControlEvents:UIControlEventTouchUpInside]; // -------- Edit here -----
self.navigationItem.backBarButtonItem = aBarButtonItem;
}
-(void)backBtnPressed
{
[self.navigationController popViewControllerAnimated:YES];
}

Related

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

Customize navigation bar button in iOS6

Update - One Possible Solution
Based on answer given by Alexander Merchi in this post (UIBarButtonItem Custom view in UINavigationBar), a solution is found. But still don't know how to change the position of the image properly.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 28.0f, 28.0f)]; // 28 points is the width or height of the button image
[btn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(onClickMenuButton) forControlEvents:UIControlEventTouchUpInside];
btnMenu = [[UIBarButtonItem alloc] initWithCustomView:btn];
Original Post
In iOS 6, the goal is like this:
while current result is like this:
My code is
btnMenu = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"button.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(onClickMenuButton)];
button.png is this the white circle with white three bars inside and a tranparent background.
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton setImage:[UIImage imageNamed:#"Button-normal"] forState:UIControlStateNormal];
[rightButton setImage:[UIImage imageNamed:#"logout-hover"] forState:UIControlStateHighlighted];
[rightButton addTarget:self action:#selector(logOut) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.leftBarButtonItem = rightBarButtonItem;
I am accomplishing it by
UIImageView *uView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"btn_back_ontouch_scr11.png"]];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
leftButton.bounds = CGRectMake( 0, 0, uView.frame.size.width, uView.frame.size.height );
[leftButton setImage:uView.image forState:UIControlStateHighlighted];
[leftButton setImage:[UIImage imageNamed:#"btn_back_normal_scr11.png"]forState:UIControlStateNormal];
[leftButton addTarget:self.navigationController action:#selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
self.scrollView.indicatorStyle=UIScrollViewIndicatorStyleWhite;
self.navigationItem.leftBarButtonItem = aButton;
self.navigationItem.hidesBackButton = YES;
This should work.

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar
Not written again in every interface.
Is there a unified method to uniform setting?
UIImage image = [UIImage imageNamed:imagePath];
UIButton button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
A lot of interface backBarButtonItem need custom,code is the same,I don't want to write N times
You can add this in a singleton global-variables file, like
GlobalVariable.h
+(UIBarButtonItem*)customButton;
GlobalVariable.m
+(UIBarButtonItem*)customButton{
UIImage *image = [UIImage imageNamed:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* btn = [[UIBarButtonItem alloc] initWithCustomView:button];
return btn;
}
Then, import GlobalVariable.h in the views you want the button to appear and call
self.navigationItem.leftBarButtonItem = [GlobalVariable customButton];
You can keep GlobalVariable there, in case you need other variables too.

How to align the title on backbuttonitem

i'm new, so i cann't upload the image, but i have a problem when i customize the backButtonItem in iOS
the code i write:
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"backBtn.png"];
backBtn.frame = CGRectMake(0, 0, backBtnImage.size.width, backBtnImage.size.height);
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
backBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
[backBtn addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[backBtn setTitle:#"Back" forState:UIControlStateNormal];
UIBarButtonItem *backBtnItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
[backBtnItem setStyle:UIBarButtonItemStyleDone];
self.navigationItem.leftBarButtonItem = backBtnItem;
but the title with "back" is not aligned on the backButton.
How could i align the title of a backbuttonitem which has been customized?
Try with this:
backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

iPad develop:customized button on navigationBar can't see after push->pop

iPad app:I've add some customized button to navigationBar as follows:
- (void)addEditButton{
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, 50, 30);
[editButton setBackgroundImage:[[UIImage imageNamed:#"images_iPad.bundle/NavigationBar_Btn_iPad.png"] stretchableImageWithLeftCapWidth:15/2.0 topCapHeight:32/2.0] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:#"Edit" forState:UIControlStateNormal];
editButton.titleLabel.font = [UIFont fontWithName:kAPPBOLDFONT size:12];
editButton.titleLabel.shadowColor = [UIColor blackColor];
editButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
UIBarButtonItem *editItem = [[[UIBarButtonItem alloc] initWithCustomView:editButton] autorelease];
self.navigationItem.rightBarButtonItem = editItem;
}
My problem is that:I clicked this button,it will push to another viewController, after popModelViewController:animate,I can't see this customized button, although this, it does work.and not just here ,others are the same,please help me.

Resources