adding buttons to a bottom toolbar - iphone

I have the following code:
self.shareButton=[[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 35, 35)];
[shareButton setBackgroundColor:[UIColor blueColor]];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_button"] forState:UIControlStateNormal];
[shareButton setBackgroundImage:[UIImage imageNamed:#"share_button_pressed"] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:#selector(shareButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.navigationController setToolbarItems:[NSArray arrayWithObject:shareButton] animated:YES];
Why I cannot see toolbar buttons? What's the problem?

What meggar says is probably the right answer. Try doing it this way.
--UPDATE--
Since the problem still exists I have updated the example to move the set call to the view controller instead of the navigation controller.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareButton];
[self setToolbarItems:[NSArray arrayWithObject:barButtonItem] animated:YES];
[barButtonItem release];

Related

ios - Not adding backbutton to NavigationBar on ViewController

I can not add on NavigationBar backbutton. I want to perform this operation on a ViewController, I have tried different ways, but could not reach a solution. Do you need to use Backbutton NavigationController to use, or you can perform on a ViewController?
Waiting for your help.
If your ViewController is within a navigation controller, you should be able to do this:
[self.navigationController popViewControllerAnimated:YES];
Hide the default back button
[self.navigationItem setHidesBackButton:YES animated:YES];
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"back_arrow.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 35, 23);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
- (void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}

Navigation Bar Button Hide When animation set as YES

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.

How to set image of navigation item (like send button) in MFMailComposer Controller

I want to set image of navigation item (send button) in MFMailComposer Controller,and i write code this..
[picker.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:#"sendbutton.png"]];
but it's not change it.I change becoz my send button not working using this code..
UIBarButtonItem *sendBtn=picker.navigationBar.topItem.rightBarButtonItem;
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeCustom];
btn2.frame=CGRectMake(280, 2, 55, 30);
[btn2 setImage:[UIImage imageNamed:#"images (2).jpeg"]forState:UIControlStateNormal];
btn2.backgroundColor=[UIColor clearColor];
[btn2 addTarget:sendBtn.target action:sendBtn.action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnTemp2=[[UIBarButtonItem alloc]initWithCustomView:btn2];
[[[[picker viewControllers]lastObject] navigationItem] setRightBarButtonItem:btnTemp2];
But my cancel button have code
UIBarButtonItem *cancelBtn= picker.navigationBar.topItem.leftBarButtonItem;
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(20, 2, 60, 30);
[btn1 setImage:[UIImage imageNamed:#"btn_tellAFriend_cancel#2x.png"]forState:UIControlStateNormal];
btn1.backgroundColor=[UIColor clearColor];
[btn1 addTarget:cancelBtn.target action:cancelBtn.action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnTemp = [[UIBarButtonItem alloc] initWithCustomView:btn1];
[[[[picker viewControllers] lastObject] navigationItem] setLeftBarButtonItem:btnTemp];
[btnTemp release];
It work fine
MFMailComposeViewController send button cannot be modified and should not be changed according to apple
The only thing you can do is change MFMailComposeViewController navigation bar tint color which will also change send button color
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[[picker navigationBar] setTintColor:[UIColor redColor]];
Try this Code.
[self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"navbar_logo.png"]];
[imageView sizeToFit];
imageView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
UIBarButtonItem *loadingView = [[UIBarButtonItem alloc] initWithCustomView:imageView];
loadingView.target = self;
self.navigationItem.rightBarButtonItem = loadingView;
[imageView release];
[loadingView release];

iPhone: Add button on MPMoviePlayerViewController's navigation bar

I am trying to add button on MPMoviePlayerViewController's navigationcontroller.view. I have wrote following so far. It doesn't give any error but button isn't appearing on view! Could anyone please tell me what am I doing wrong? Thanks.
MPMoviePlayerViewController *videoController = [[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:filePath]] autorelease];
UIButton *favButton = [UIButton buttonWithType:UIButtonTypeCustom];
favButton.frame = CGRectMake(280, 25, 30, 30);
[favButton addTarget:self action:#selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[favButton setBackgroundImage:[UIImage imageNamed:#"unselected.png"] forState:UIControlStateNormal];
[videoController.navigationController.view addSubview:favButton];
[self presentMoviePlayerViewControllerAnimated:videoController];
- (void)favouriteButtonClicked:(id)sender
{
NSLog(#"Inside favourite button clicked");
}
Also try adding using following code but no luck!
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(favouriteButtonClicked:)];
videoController.navigationController.navigationItem.rightBarButtonItem=button;
[button release];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(favouriteButtonClicked:)];
videoController.navigationItem.rightBarButtonItem = button;
[button release];
If you use presentModalViewController, you can not see the button you add. You have to [self.navigationController pushViewController:moviePlayer animated:YES] to see it.
Try this..it will work
[[[UIApplication sharedApplication] keyWindow] addSubview:favButton];

UIImage in setBackBarButtonItem in UINavigationBar iphone

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];
}