UIBarbuttonItem disappears - objective-c

I have the following function which displays the top bar on my app. It consists of an image and a UIBarButton. I call it from viewWillAppear. When I push the current controller and move to the next controller and then move Back to the main controller, the right bar button disappears. Please help.
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
[rightButton release];
}
[titleImage release];
}

-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
// [rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
//[rightButton release];
}
// [titleImage release];
}

Related

Cannot push back to view controller embed in Navigation Controller

I have created ViewController A (a.k.a vcA) and vcB embed in Navigation View Controller ,mapping child view controller in storyboard as classes.m
When I try to construct this redirect of view controller A->B->A , or to create vcA from vcB , by using :
A :
PairViewController * sliderVC = [self.storyboard instantiateViewControllerWithIdentifier:#"PairViewController"];
sliderVC.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:sliderVC animated:NO completion:nil];
sliderVC.view.backgroundColor = [UIColor clearColor];
B : ViewController *destinationController = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewController"];
[destinationController setBle:_ble];
[destinationController setLeDevice:selectedDevice];
destinationController.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:destinationController animated:NO completion: nil] ;
The new A is created without any navigation bar appeared.
When I use
[self.navigationController pushViewController:destinationController animated:nil];
The xCode said for the exception ;
Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller.'
Would you please tell me the way to create vcA from vcB , with my custom navigation bar keeping intact , not missing ?
In A I try to construct the navigation bar in this method but navigation bar turns nil if creating vcA from vcB
-(void)viewWillLayoutSubviews{
navigationBar= [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
}
- (void)viewWillAppear:(BOOL)animated {
[self.navigationController setNavigationBarHidden:NO];
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:#"FPV Control"];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 20)];
[button setImage:[UIImage imageNamed:#"menu_back.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// UIBarButtonItem *buttonItemA = [[UIBarButtonItem alloc] initWithCustomView:button];
UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(buttonClicked:)];
[leftBarButtonItem setCustomView:button];
navigationItem.leftBarButtonItem = leftBarButtonItem;
UIButton *buttonA = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 20)];
[buttonA setImage:[UIImage imageNamed:#"rc_logo.png"] forState:UIControlStateNormal];
UIBarButtonItem *buttonItemB = [[UIBarButtonItem alloc] initWithCustomView:buttonA];
navigationItem.rightBarButtonItem = buttonItemB;
[navigationBar pushNavigationItem:navigationItem animated:NO];
NSLog(#" navigaytion item : %#" , navigationItem);
NSLog(#" navigaytion bar : %#" , navigationBar);
So present your ViewController with NavigationController as below.
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:myViewController];
//now present this navigation controller modally
[self presentViewController:navigationController
animated:NO
completion:nil];

How do I correctly set the tint colour of a UINavigationBar in iOS7?

I'm using the last two lines of the code below to change the tint colour of my navigation bar so that my UIBarButtonItems display as black instead of the default blue. This code works in another controller but not in this controller.
When I navigate forward to that controller then navigate back to the controller in question then the UIBarButtonItems are black as needed. However when I first load the app and this view is loaded they are blue.
How do I correctly change the tint colour of my navigation bar so my UIBarButtonItem's display as black?
The actual images I've set for the buttons are black.
Code in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Logo button
UIButton *logoButton = [[UIButton alloc] init ];
[logoButton setImage:[UIImage imageNamed:#"va_logohme.png"] forState:UIControlStateNormal];
[logoButton setAdjustsImageWhenHighlighted:NO]; ;
[logoButton setFrame:CGRectMake(0, 0, 320, 40)];
[logoButton addTarget:self action:#selector(logoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[[self navigationItem] setTitleView:logoButton];
// Additional UIBarButtonItem's
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"hamburger_for_more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuButtonTap)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"magnify_glass.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTapped)];
[[self navigationItem] setLeftBarButtonItems:#[menuButton, searchButton]];
UIBarButtonItem *shoppingCartButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"shopping_bag.png"] style:UIBarButtonItemStylePlain target:self action:#selector(shoppingCartButtonTapped)];
UIBarButtonItem *addFavouriteButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"add_fav_heart.png"] style:UIBarButtonItemStylePlain target:self action:#selector(favouritesPageButtonTapped:)];
NSMutableArray *buttons = [[NSMutableArray alloc] init];
for (UIControl *btn in self.navigationController.navigationBar.subviews) {
if ([btn isKindOfClass:[UIControl class]]) {
[buttons addObject:btn];
}
}
// Basket container
_bagContainer = [[UIView alloc] initWithFrame:CGRectMake(277, 16, 21, 21)];
[[[self navigationController] navigationBar] addSubview:_bagContainer];
UILabel *bagCount = [[UILabel alloc] init];
[bagCount setText: [NSString stringWithFormat:#"%i", [Bag totalItems:[self managedObjectContext]]]];
[bagCount setFont:[UIFont systemFontOfSize:14]];
[bagCount sizeToFit];
[_bagContainer addSubview:bagCount];
bagCount.center = [_bagContainer convertPoint:_bagContainer.center fromView:_bagContainer.superview];
[_bagContainer setUserInteractionEnabled:NO];
[[self navigationItem] setRightBarButtonItems:#[shoppingCartButton, addFavouriteButton] animated: YES];
// Change colour of nav bar tint
[[[self navigationController] navigationBar] setBarTintColor:[UIColor whiteColor]];
[[[self navigationController] navigationBar] setTranslucent:NO];
}
If the images are template images, then set their tintColor to the desired color. If they are normal images, then make sure to render them as normal images so that they show up in their own color and not as tinted template images.

How actions to Next/Previous Keyboard ToolbarCustom iOS7

I am trying to set the next/previous buttons on my keyboard toolbar, I want get put in navigation to go next/Previous textfield. Here is what I am trying.
How can I use the system bar button item to change to textfield?
-(void)addTeam{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(15, (0+(50*[teamsArray count])), 273, 50)];
[view setTag:[teamsArray count]+1];
UILabel *lblTeamName = [[UILabel alloc ] initWithFrame:CGRectMake(5,0,58,20)];
[lblTeamName setBackgroundColor:[UIColor clearColor]];
lblTeamName.font = [UIFont fontWithName:#"Arial Rounded MT Bold" size:(12.0)];
lblTeamName.text = [NSString stringWithFormat:#"Team %i",[teamsArray count]+1];
[lblTeamName setTag:7];
[view addSubview:lblTeamName];
UITextField* txtTeamName = [[UITextField alloc]initWithFrame:CGRectMake(5,20,150,30)];
[txtTeamName setBorderStyle:UITextBorderStyleRoundedRect];
[txtTeamName setPlaceholder:#"Team Name"];
[txtTeamName addTarget:self action:#selector(hideKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[txtTeamName setDelegate:self];
[view addSubview:txtTeamName];
UIButton *btnDelete = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnDelete addTarget:self
action:#selector(btnDelete_OnTouch:)
forControlEvents:UIControlEventTouchDown];
[btnDelete setTag:[teamsArray count]];
[btnDelete setTitle:#"-" forState:UIControlStateNormal];
btnDelete.frame = CGRectMake(160, 20, 20, 30);
[view addSubview:btnDelete];
[scrollView addSubview:view];
[teamsArray addObject:view];
}
-(BOOL)textFieldShouldBeginEditing: (UITextField *)textField
{
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;
}
- (void)nextTextField {
????
}
-(void)previousTextField
{
????
}
you could use next and previous button,
How to navigate through textfields (Next / Done Buttons)
And do this in ViewDidLoad
UIToolbar * keyboardToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
keyboardToolBar.barStyle = UIBarStyleDefault;
[keyboardToolBar setItems: [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(previousTextField)],
[[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(nextTextField)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard)],
nil]];
textField.inputAccessoryView = keyboardToolBar;

How to add button to navigation controller

Here is my code..I am unable to add a button in my navigation controller.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1, *viewController2, *viewController3,*viewController4;
viewController1 = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
viewController2 = [[DisplayAllImagesController alloc] initWithNibName:#"DisplayAllImagesController" bundle:nil];
viewController3 = [[EmptyView alloc] initWithNibName:#"EmptyView" bundle:nil];
viewController4 = [[ListView alloc] initWithNibName:#"ListView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController2, viewController1, viewController3, viewController4, nil];
self.tabBarController.title = #"Title";
self.navController = [[UINavigationController alloc]
initWithRootViewController:self.tabBarController];
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
So,please let me know how i can add button to navigation controller...please...
just add this bellow code in your ViewController's viewDidLoad: method...
UIBarButtonItem *btnReload = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(btnReloadPressed:)];
self.navigationController.topViewController.navigationItem.rightBarButtonItem = btnReload;
btnReload.enabled=TRUE;
btnReload.style=UIBarButtonSystemItemRefresh;
Try this:
UIButton *customButton = [UIButton alloc] initWithFrame:CGRectMake(0, 0, 50,30)];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.rightBarButtonItem = closeButton;
if you want custom button we can use this
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton1 setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
myButton1.showsTouchWhenHighlighted = YES;
myButton1.frame = CGRectMake(0.0, 3.0, 50,30);
[myButton1 addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton1];
self.navigationItem. rightBarButtonItem = rightButton;
//create the button and assign the image
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
[rightButton setFrame:CGRectMake(280,7,buttonImage.size.width,buttonImage.size.height)];
[rightButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
rightButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[rightButton setTitle:#"Right" forState:UIControlStateNormal];
// add image view
[self.navigationController.navigationBar addSubview:rightButton];
[self.navigationController.navigationBar bringSubviewToFront:rightButton];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarDone = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
self.navigationItem.rightBarButtonItem = customBarDone;
[rightButton addTarget:self action:#selector(yourSelector:) forControlEvents:UIControlEventTouchUpInside];
UIButton* btn =[UIButton alloc] init];
....
self.navController.view addsubview:btn];

how do you add a right button to a uinavigationcontrol

How can I add a button to a UINavigationControl that I added to a ViewController? (The project isn't UINavigationControler based.)
Here's the code I've added to my viewDidLoad method:
navigation = [[UINavigationController alloc] init];
navigation.view.frame = CGRectMake(0, 0, 1024, 748);
navigation.navigationBar.tintColor = [UIColor blackColor];
navigation.navigationBar.alpha = 1.0f;
navigation.navigationBar.translucent = NO;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
[navigation.navigationItem setRightBarButtonItem:cancelButton animated:YES];
[cancelButton release];
[self.view addSubview:navigation.view];
[self pushPresentationList];
Can someone help me fix lines 7-9 please?
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Button"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Awesome"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
Do you need an UINavigationController? Why not just use a simple UINavigationBar. Here is some sample code:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width, 49.0f)];
[self.view addSubview:navBar];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
UINavigationItem *rightBarButtonItem = [[UINavigationItem alloc] init];
[rightBarButtonItem setRightBarButtonItem:cancelButton];
[navBar pushNavigationItem:rightBarButtonItem animated:NO];