reusable UIBarButtonItem in all View Controllers in Objective C - ios

I making custom navigationBar with background color and font size. In addition i have add menu button on right. For this I had made the category named
UINavigationController+Transparent.h
#interface UINavigationController (Transparent)
- (void)presentTransparentNavigationBar;
- (void)hideTransparentNavigationBar;
#end
UINavigationController+Transparent.m
#import "UINavigationController+Transparent.h"
#implementation UINavigationController (Transparent)
UIBarButtonItem *menuButton;
- (void)presentTransparentNavigationBar;
{
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menu_icon"] style:UIBarButtonItemStylePlain target:self action:#selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:#selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
[self.navigationBar setTranslucent:NO];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBarTintColor:[UIColor wolfRed]];
[self.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:19.0] }];
[self setNavigationBarHidden:NO animated:NO];
}
- (void)hideTransparentNavigationBar
{
[self setNavigationBarHidden:YES animated:NO];
[self.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
#end
inside my ViewControllers i call
[self.navigationController presentTransparentNavigationBar];
or
[self.navigationController hideTransparentNavigationBar];
The problem is that menu button is not visible and back button is seems like standard iOS blue back button.
Thanks for help!

See I used to customize my navigation bar in following way with
objective c
Step.1 Make sure your View Controller is embed in Navigation Controller
Step.2 Add required icons in your assets eg. "menu_icon" and "back"
Step.3 with the objective-C you can customize navigation bar as like you want.
I always do this way,
#import "ViewController.h"
#interface ViewController (){UIBarButtonItem *menuButton;}
#end
#implementation ViewController
#pragma mark - life cycle methods
- (void)viewDidLoad {[super viewDidLoad];}
- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
#pragma mark - IBAction methods
- (IBAction)showClicked:(id)sender {[self presentTransparentNavigationBar];}
- (IBAction)hideClicked:(id)sender {[self hideTransparentNavigationBar];}
#pragma mark - Navigation Bar methods
//Customize Naviagtion Bar
- (void)presentTransparentNavigationBar;
{
//Add Menu Button to Navigaiton Bar
menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"menu_icon"] style:UIBarButtonItemStylePlain target:self action:#selector(showMenu:)];
[menuButton setTintColor:[UIColor whiteColor]];
self.navigationItem.rightBarButtonItem = menuButton;
//Add Back button to Navigation Bar
UIImage *backButtonImage = [UIImage imageNamed:#"back"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage
forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self
action:#selector(popViewController)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
//Config Navigaton bar settings
[self.navigationController.navigationBar setTranslucent:NO];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];//I used light gray color
[self.navigationController.navigationBar setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont fontWithName:#"Helvetica" size:19.0] }];
[self.navigationController.navigationBar setHidden:NO];
}
//Hide Navigation Bar
- (void)hideTransparentNavigationBar
{
[self.navigationController.navigationBar setHidden:YES];
[self.navigationController.navigationBar setBackgroundImage:[[UINavigationBar appearance] backgroundImageForBarMetrics:UIBarMetricsDefault] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setTranslucent:[[UINavigationBar appearance] isTranslucent]];
[self.navigationController.navigationBar setShadowImage:[[UINavigationBar appearance] shadowImage]];
}
- (void) showMenu:(id) sender{ /*TODO when clicks on Menu button */ }
- (void) popViewController{ /*TODO when clicks on Back button */ }
#end
and if managed everything proper you should be able to get Menu and Back button to navigation bar when you click show button on the screen.
Note:
you can do this in your AppDelegate file so it will available in entire application

Related

My back button cannot exit my app

I am now using my navigation Item to create exit button (left navigation item). When it comes to creating the view controller embed in navigation view controller , it seems that we cannot exit the app by using popViewControllerAnimated and dismissViewControllerAnimated . WOuld you please tell me what to do ?
The below is my code for embed view controller
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
navigationBar = self.navigationController.navigationBar;
[navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:#"TitilliumText22L-Medium" size:22.0], NSFontAttributeName,
nil] ];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"TEST SSS"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
[button setImage:[UIImage imageNamed:#"menu_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItemA;
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 88, 30)];
[buttonA setImage:[UIImage imageNamed:#"sss.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
navigationItem.rightBarButtonItem = buttonItemB;
[navigationBar pushNavigationItem:navigationItem animated:NO];
}
- (UIStatusBarStyle) preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonClicked:(id)sender {
NSLog(#"ssd finish");
[self.navigationController popViewControllerAnimated:YES|NO];
}
#end
Please read the document on this link, Apple Q&A Link for quit iOS app
This should answer your question:-
Go to your info.plist and check the key
"Application does not run in background"
and set it to :- "TRUE"
Then the time user presses the home button, the application exits completely.
OR as the link suggests pop an alert so that the user will know what to do for exiting the app.

Why does my custom Navigation bar back button overlap/duplicated (Xcode 7.0)?

I added the following to customize my back button in my navigation bar but the image just overlaps/duplicated:
http://i.stack.imgur.com/eyzJC.png
- (void)viewWillAppear:(BOOL)animated
{
//take out the label following the button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
//set image for back button
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:#"backButtonImage.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:#"backButtonImage.png"]];
self.navigationItem.backBarButtonItem = backButton;
}
It's supposed to be an image of one black arrow facing to the left.
Before it worked fine but now it does this when I simulate it. Any insight and help would be appreciated.
Thank you
This should work for you.
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
back.frame = CGRectMake(0, 0, 50, 44);
back.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[back addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
[back setImage:[UIImage imageNamed:#"backButtonImage.png"] forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: back];
add a function:
-(void) back
{
[self.navigationController popViewControllerAnimated:YES];
}
Add this code
[UINavigationBar appearance].barTintColor = [UIColor colorWithWhite:1.00 alpha:1];
It should solve your problem.

Title of UINavigationBar becomes blank when segueing back to root VC

I've got a UINavigationController that segues to 2 other VC's. Whenever it reaches a certain VC with the current setup :
// set text
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.title = #"Title";
// make the nav bar transaparent
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
[self.navigationController.navigationBar setOpaque:NO];
// set white status bar
self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
[self setNeedsStatusBarAppearanceUpdate];
//self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(16, 31, 22, 20)];
UIImage *backImage = [UIImage imageNamed:#"backButtonDetail"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
And I pop the VC, the title of the root vc becomes empty. However, whenever I'm segueing to a VC without this setup, but a more 'basic' setup, the title stays where it should.
Easiest thing I can recommend: use [UINavigationBar appearance] to set many of these customizations from the AppDelegate
Checkout out AppCoda's post on the subject. I've just run through it myself and it's pretty great

UINavigationBar - Change Height / Add Big Button

I need to change the height of my Navigation Bar and add a custom Image button to the top left corner. I am part way there, but lost now on getting the custom Image button in the right position. Here is what I have:
To adjust the height I have created a UINavBar category with one method as follows:
#implementation UINavigationBar (myNavBar)
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(768,80);
return newSize;
}
#end
I have also created a UINavigationController subclass to modify the button. Here is the viewDidLoad from that class:
UIImage *navBackgroundImage = [UIImage imageNamed:#"bar"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:#"back_off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 13, 0, 6)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
// Change the appearance of other navigation button
UIImage *barButtonImage = [[UIImage imageNamed:#"menu_off"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
So far this solution resizes the top nav bar, but positions my button in a weird position. Here are what I want vs. what is happening:
What I want
What I get
I have a UIBarButtonItem Category that I use which has an offset property:
UIBarButtonItem+CustomImage.h
#interface UIBarButtonItem (CustomImage)
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action offset:(CGPoint)offset;
#end
UIBarButtonItem+CustomImage.m
#import "UIBarButtonItem+CustomImage.h"
#implementation UIBarButtonItem (CustomImage)
+ (UIBarButtonItem *)barItemWithImage:(UIImage *)image target:(id)target action:(SEL)action offset:(CGPoint)offset {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[button setBounds:CGRectOffset(button.bounds, 0.0, -10.0)];
UIView *container = [[UIView alloc] initWithFrame:button.frame];
[container setBounds:CGRectOffset(container.bounds, offset.x, offset.y)];
[container addSubview:button];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];
return item;
}
#end
Example Usage
#import "UIBarButtonItem+CustomImage.h"
UIBarButtonItem *settingsButton = [UIBarButtonItem barItemWithImage:settingsImage
target:self
action:#selector(revealSettings:)
offset:CGPointMake(0.0, 0.0)];
[self.navigationItem setLeftBarButtonItem:settingsButton];

ios 5 change the background of back button in navigation controller to transparent

I have customised the navigation controller title bar with a background image, but I am really struggling to change the background color of the back button to transparent so that it matches with the green title bar beneath it. I am fairly new to iOS development. Can any one suggest what could be done?
Here is the code for I used to change the title bar of navigation controller, just in case it helps:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"greenbar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
// [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
//change back button image
}
EDIT: After doing a bit of research I managed to get what I wanted. Here is the code to change the background image for the back button:
UIImage *image1 = [UIImage imageNamed:#"back-bt.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
The above code adds the image to all the back buttons in the navigation controller.
You can't change the appearance of the default back button, but you can create your own button to replace it.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"greenbar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
// [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
//change back button image
if(self.navigationController.viewControllers.count > 1) {
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
backButton.frame = CGRectMake(0.0f, 0.0f, 64.0f, 41.0f);
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
}
- (void) didTapBackButton:(id)sender {
if(self.navigationController.viewControllers.count > 1) {
[self.navigationController popViewControllerAnimated:YES];
}
}

Resources