I am adding image in BackBarButtonItem of navigation bar, image gets in button but image is not scale to fill what would be the issue.
Here is the code i am using and it displays in following way.
UIImage *backImage = [UIImage imageNamed:#"back.png"];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:#selector(backAction)];
[self.navigationItem setBackBarButtonItem: newBackButton];
[newBackButton release];
[backImage release];
Actually it should look like, below image.
Thanks!
[self.navigationItem setHidesBackButton:YES];
[self.navigationItem setLeftBarButtonItem:newBackButton];
Try this :)!
problem is your image size.check it!
You can also use
//custom back button
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(backButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(-2, 0, 52, 30)];
UIBarButtonItem *btnItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = btnItem;
[btnItem release];
}
Related
I am setting Back and next Button as Navigation Bar item left button and right button.which have code below as
// Back Button
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(navigatehome)forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = aBarButtonItem;
//Next Button
aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:#selector(navigatenext)forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:aBarButtonItem]
And Problem when animated:YES in statement
CSA_info *h1=[[CSA_info alloc]init];
[self.navigationController pushViewController:h1 animated:YES];
It shows as below image
If uses animated:No then showed as below
I am adding a image in as subview in navigation Bar so it hides the buttons.
UIImage *image = [UIImage imageNamed:#"title bar.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(-5, 0, 330, 44);
[self.navigationController.navigationBar insertSubview:imageView atIndex:2];
I required to add that image in navigation.
Why it so, Any Help ?
you need to put title bar image in secondViewcontroller like bellow in ViewDidLoad
UIImage* imageback = [UIImage imageNamed:#"Back_Button.png"];
CGRect frameimgback = CGRectMake(0, 0, 50, 30);
backButton = [[UIButton alloc] initWithFrame:frameimgback];
[backButton setBackgroundImage:imageback forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(Back)
forControlEvents:UIControlEventTouchUpInside];
UIImage *image = [UIImage imageNamed:#"titileBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = btn;
If I understood you problem properly then following solution should work,
You need to add the code in viewDidLoad method of the view controller. Please refer the below code
UIButton *backButton = [[UIButton alloc] initWithFrame:kButtonFrame];
[backButton setImage:[UIImage imageNamed:kBackButtonImage] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(navigatehome)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = aBarButtonItem;
UIButton *nextButton = [[UIButton alloc] initWithFrame:kButtonFrame];
[nextButton setImage:[UIImage imageNamed:kNextButtonImage] forState:UIControlStateNormal];
[nextButton addTarget:self action:#selector(navigatenext)forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *aBarButtonItemRight = [[UIBarButtonItem alloc] initWithCustomView:nextButton];
[self.navigationItem setRightBarButtonItem:aBarButtonItemRight];
This should work fine even you animated while pushing the view controller.
i have UITableView based application, and i can't set backBarButtonItem properly.
Back button is button, which appears when i select any item, call second view with method didSelectRowAtIndexPath. When i try to set it like this:
UIButton *back1=[UIButton buttonWithType:UIButtonTypeCustom];
[back1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[back1 addTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
[back1 setImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:back1];
self.navigationItem.backBarButtonItem = back;
There is just back button with default style and somehow with title of my TableView controller window.
When i set it like this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"back_button.png"]
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
self.navigationItem.backBarButtonItem = backButton;
There is an image, but, it look like "inserted" in default back button, doesn't look nice at all.
Please help me.. i tried many ways to solve this, but, in many cases there is just default Back button with title "Products" (there is title which appears on top of navigation bar in my UITableView). Any help would be appreciated!
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
backButton.font = [UIFont boldSystemFontOfSize:12];
backButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[backButton addTarget:self action:#selector(showMeter)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:backButton];
//Create resizable images
UIImage *myButton = [[UIImage imageNamed:#"yourButton"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
//Set the background image for *all* UIBackBarButtons
[[UIBarButtonItem appearance] setBackgroundImage:myButton forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
You should assign it to self.navigationItem.leftBarButtonItem.
self.navigationItem.leftBarButtonItem = backButton;
When One of my app navigate from one controller to another, the 'go back' UIBarButtonItem disappear, so I wrote codes:
UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStyleDone
target:self
action:#selector(barButtonItemPressed:)];
self.navigationItem.leftBarButtonItem = barButton1;
[barButton1 setImage:[UIImage imageNamed:#"fdj.png"]];
[barButton1 release];
It works, but it display as
but I prefer the style (like standard go back barbuttonitem
Is it possible?
Welcome any comment
You should edit the navigationItem of the parent view controller:
You can place this in the viewDidLoad of the previous view controller:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
self.navigationItem.backBarButtonItem.image = [UIImage imageNamed:#"fdj.png"];
here is the code how you can do this
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0, 0, 55, 36);
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateNormal];
[btnBack setImage:[UIImage imageNamed:#"btnBack"] forState:UIControlStateSelected];
[btnBack addTarget:self action:#selector(onClickBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = item4;
just make image and set it in uibutton, it's simple way to do this.
you can set any image you want.
hope this may help you what you want.
In the parent view controller's init method, set
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithImage: [UIimage imageNamed: #"fdj.png"]
style: UIBarButtonItemStyleBordered
target: nil
action: nil] autorelease];
This is what I'm using in an older project of mine (and yes, I know that stretchableImageWithLeftCapWidth is deprecated).
#implementation UIViewController (UIView_ExtenderClass) // this is a category, obviously
...
-(void)setLeftButton:(id)theTarget navItem:(UINavigationItem *)myNavItem action:(SEL)myAction title:(NSString *)myTitle;
{
CGSize titleWidth = [myTitle sizeWithFont:[UIFont boldSystemFontOfSize:14.0]];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, titleWidth.width + 20, 32)];
[button setTitle: [NSString stringWithFormat:#" %#", myTitle] forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0];
[button addTarget:theTarget action:myAction forControlEvents:UIControlEventTouchUpInside];
UIImage *imgBack = [UIImage imageNamed:#"32_left_nav_bar.png"];
UIImage *imgBackStretched = [imgBack stretchableImageWithLeftCapWidth:15 topCapHeight:0];
UIImage *imgBackSelected = [UIImage imageNamed:#"32_left_nav_bar_selected.png"];
UIImage *imgBackSelectedStretched = [imgBackSelected stretchableImageWithLeftCapWidth:15 topCapHeight:0];
[button setBackgroundImage:imgBackStretched forState:UIControlStateNormal];
[button setBackgroundImage:imgBackSelectedStretched forState:UIControlStateHighlighted];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttonItem setTarget:theTarget];
myNavItem.leftBarButtonItem = nil;
myNavItem.leftBarButtonItem = buttonItem;
[imgBack release];
[imgBackSelected release];
[button release];
[buttonItem release];
}
Call it in the viewDidLoad of the second controller, like this:
[self setLeftButton:self navItem:self.navigationItem action:#selector(yourActionHere) title:#"Back"]
The image I'm using is here: http://i49.tinypic.com/6qfzfs.png
I want to add a custom button (to the right) in my navigation bar.
I have the following code in my "viewDidLoad" function :
UIButton *audioBtn = [[UIButton alloc] init];
[audioBtn setTitle:#"Play" forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"play.png"];
[audioBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
I can't see the button in my navigation bar, but if I click to the right (where the button is supposed to be), it triggers the function "toggleAudioPlayback", so the only problem is that I don't see the button!
I tried with different image, setting a background color, nothing works...
By the way, I use this image somewhere else in the code, and I see it (on a custom button, but not in a navigationBar).
Help me please!
Make your life easier and instead of a customView just use a UIBarButtonItem with a custom image:
UIBarButtonItem *audioBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"play.png"] style:UIBarButtonItemStylePlain target:self action:#selector(toggleAudioPlayback:)];
self.navigationItem.rightBarButtonItem = audioBtn;
[audioBtn release];
UPDATE
Since you have indicated you want your button to not have a border, then you will need to use a CustomView afterall, but I have modified your code to make this work (the key difference is the assigning of the frame, which is set to the size of the image, but you could set it to a custom size to have the image centered):
UIButton *audioBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *playImg = [UIImage imageNamed:#"play.png"];
[audioButton setBackgroundImage:playImg forState:UIControlStateNormal];
[audioButton setBackgroundImage:playImg forState:UIControlStateHighlighted];
audioBtn.frame = CGRectMake(0,0,playImg.size.width,playImg.size.height);
[audioBtn addTarget:self action:#selector(toggleAudioPlayback:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:audioBtn];
self.navigationItem.rightBarButtonItem = button;
[audioBtn release];
[button release];
Instead of [[UIButton alloc] init] try the +buttonWithType: method on UIButton.
I have a Tab bar application. and in one of the tab bars I have a navigation contorller.
Im trying to set a UIButton with an image as the right button the navigation bar.
UIButton *refreshButton = [[UIButton alloc] init];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refreshSection) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];
//[rightButton setImage:[UIImage imageNamed:#"refresh_icon.png"]]; << DOESN'T WORK EITHER
self.navigationItem.rightBarButtonItem = rightButton;
[refreshButton release];
[rightButton release];
but the image doesn't show up. I get an invisible button.
EDIT:
I want the button style to be plain or background to be transparent, so that only the image shows. Hence I am using a UIbutton instead of using a UIBarButton directly.
You don't have to create a UIButton and use it as custom view in the UIBarButtonItem. You can create a UIBarButtonItem directly with an image:
UIImage *myImage = [UIImage imageNamed:#"myImage.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStyleBordered target:self action:#selector(refreshSection)];
self.navigationItem.rightBarButtonItem = button;
[button release];
--
Updated, according to comments
Use a UIImageView as custom view in the UIBarButtonItem:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myImage.png"]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:imageView];
self.navigationItem.rightBarButtonItem = rightButton;
[imageView release];
[rightButton release];
--
Updated
Tried the last approach and while this does visually look like you want it to, I can't seem to get it to handle taps. Although the target and action has been set.
--
Final update
I've gotten your initial code in the question up & running. The reason the image is not showing, is because you haven't set the frame of the button. Once you set the frame, the image is shown in the navigation bar.
Here's my snippet I used & tested:
UIImage *myImage = [UIImage imageNamed:#"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);
[myButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
[myButton release];
Note: I have set the showsTouchWhenHighlighted property of the UIButton to visually hightlight the button when it is pressed.
I ended up doing this
UIButton *refreshButton = [[UIButton alloc] initWithFrame:CGRectMake(292, 9, 27, 27)];
[refreshButton setImage:[UIImage imageNamed:#"refresh_icon.png"] forState:UIControlStateNormal];
[refreshButton addTarget:self action:#selector(refresh) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController.navigationBar addSubview:refreshButton];
[refreshButton release];
It works fine now but I am not really sure if this is the right way of doing it.