back button presentModalViewController - objective-c

I want to have a back button when I go to another view controller but I have not been able to achieve this. I have tried to set the back button to not hidden with no luck. A little background information, there is no navigation controller being loaded when the app starts, I'am trying to go through one uiviewcontroller to another.
Here is how I have tried to achieve this, if anyone can tell what is wrong... it would be appreciated!
displayViewController *controller = [[displayViewController alloc] initWithNibName:#"displayViewController" bundle:nil];
UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController:aNavController animated:YES];
[aNavController release];
[controller release];

You can put the back button in the viewController in which you want to have the back button
- (void)viewDidLoad
{
// back button
UIImage* image = [UIImage imageNamed:#"back.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
UIButton *backButton = [[UIButton alloc] initWithFrame:frame];
[backButton setBackgroundImage:image forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction:) forControlEvents:UIControlStateHighlighted];
UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
[backButtonItem release];
[backButton release];
}
-(void) backAction:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}

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.

Display default tabBarView on tabBarItem Click event

I have used UITabbarController in my app.One of the tabBarItem is contactsViewController which displays list of contacts with UITableView.When I click on the tableRow it loads another view.then I click some other tabBarItem.again I click contactsViewController it takes me to the view where i left.It does not display default contact view.I have created UITabbarController progrmattically.How do i display default tabBarView on tabBarItem click?
tabbarController = [[UITabBarController alloc]init];
self.tabbarController.delegate = self;
tabbarView = [[UIView alloc]initWithFrame:CGRectMake(0, 431, 320, 49)];
UIImageView *tabImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 49)];
[tabImage setImage:[UIImage imageNamed:#"Taskbar.png"]];
[tabbarView addSubview:tabImage];
UIButton *tabItem1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 49)];
[tabItem1 setImage:[UIImage imageNamed:#"Btn_Home.png"] forState:UIControlStateNormal];
[tabItem1 setTag:1];
[tabItem1 addTarget:self action:#selector(tabBarBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[tabbarView addSubview:tabItem1];
-(IBAction)tabBarBtnAction:(id)sender
{
UIButton *btn = (UIButton *)sender;
// NSLog(#"tag %d\n",btn.tag);
[self resetTabBarBtnImage];
[self resetAllTabBarBtnImage];
PreviousBtnTag = btn.tag;
if ([btn tag]==1) {
tabbarView.hidden = YES;
[self.tabbarController setSelectedIndex:0];
[self.navigationController popToRootViewControllerAnimated:YES];
[btn setImage:[UIImage imageNamed:#"Btn_Home-Over.png"] forState:UIControlStateNormal];
}
else if([btn tag]==2)
{
tabbarView.hidden = NO;
[self.tabbarController setSelectedIndex:1];
[btn setImage:[UIImage imageNamed:#"Btn_Contacts-Over.png"] forState:UIControlStateNormal];
[self.navigationController popToRootViewControllerAnimated:YES];
}
In the appDelegate class , initially set the UItabbarController delegate as self
In the implememtation of appDelegate i.e .m file
//Assuming the first tab has the contactsviewController
-
(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
if (tabBarController.selectedIndex == 0) {
UINavigationController *requiredNavigationController = [tabBarController.viewControllers objectAtIndex:0];
[requiredNavigationController popToRootViewControllerAnimated:YES];
}
}
I would recommend you to create the "tab bar controller" with the Interface Builder, as it is much easier to implement these kind of things this way. Anyway, if you want to keep trying to create it programmatically, try changing this:
[self.navigationController popToRootViewControllerAnimated:YES];
for this:
RootViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"Root"];
[self.navigationController pushViewController:controller animated:YES];
If this does not work for you, why don't you use Tool Bar Items, in instead of a Tab Bar Controller? It is quite easy to manage events with Tool Bar items, as they are treated like buttons.
Good luck!

How to add button to toolbar using Three20?

I'm trying to use this to add button to toolbar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NO animated:NO];
self.navigationController.toolbar.translucent = YES;
self.navigationController.toolbar.barStyle = UIBarStyleBlack;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0.0, 0.0, 10.0, 10.0);
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *a = [NSMutableArray arrayWithObject:infoButton];
[self.navigationController setToolbarItems:a];
}
But there is no button in toolbar when I started application! :(
Instead of setting the navigation controllers toolBarItems property try setting it in the currently displayed view controller like so:
[self setToolbarItems:[NSArray arrayWithObjects:infoButton, nil]];
Also adding infoButton to the toolBarItems will automatically retain it. So remember to place
[infoButton release];
at the bottom of your viewWillAppear method.

vertically aligning UINavigationItems

I have customized the UINavigationBar's height to 100px and I'd like to add buttons onto this customized bar.
All is good except the button seems to want to sit on the bottom of the nav bar no matter what. I can't get it to align to the center or the top of the nav bar.
Here is the code; first I create a navigation controller in the app delegate and add one button on the right.
// set main navigation controller
temp *tempc = [[temp alloc] initWithNibName:#"temp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc];
UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
[tempc.navigationItem setRightBarButtonItem:navItem];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
then I resize the navigation bar in the "temp" view controller like so:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
[self.navigationController.navigationBar setFrame:frame];
[self.navigationController.navigationBar setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
}
I've also tried to add a custom view to the rightBarButtonItem but I can't get the added custom view to touch the top completely.
And the code for this unfortunate attempt:
// set main navigation controller
temp *tempc = [[temp alloc] initWithNibName:#"temp" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tempc];
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36.0f, 80.0f)];
[customView setBackgroundColor:[UIColor blueColor]];
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setFrame:CGRectMake(0, 22.0f, 36.0f, 36.0f)];
[customButton setTitle:#"+" forState:UIControlStateNormal];
[customView addSubview:customButton];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:customView];
Does anyone know how to vertically align UIBarButtonItems on a UINavigationBar?
This will move page title and all buttons up 20 px:
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault];
The current accepted answer from alhcr (Option 1) does not work if you only have a rightBarButtonItem and no left. It is also making an assumption about the subview ordering of the UINavigationBar which is not guaranteed and could very well change in a future version of iOS. Here is a better way of customizing navigation button frames:
// UINavigationBar subclass
- (void)layoutSubviews {
[super layoutSubviews];
UINavigationItem *navigationItem = [self topItem];
for (UIView *subview in [self subviews]) {
if (subview == [[navigationItem rightBarButtonItem] customView]) {
CGRect newRightButtonRect = CGRectMake(...new rect...);
[subview setFrame:newRightButtonRect];
}
else if (subview == [[navigationItem backBarButtonItem] customView]) {
CGRect newBackButtonRect = CGRectMake(...new rect...);
[subview setFrame:newBackButtonRect];
}
}
}
Option 1:
create UINavigationBar subclass
inside this class override - (void)layoutSubviews where you will reposition UIBarButtonItems
in Interface Builder set UINavigationBar class to UINavigationBar subclass
Example:
inside subclass of UINavigationBar:
- (void)layoutSubviews {
int index = 0;
for ( UIButton *button in [self subviews] ) {
if(index == 1) {
[button setFrame:CGRectMake(5,40,60,40)]; //leftBarButtonItem
} else if(index == 2) {
[button setFrame:CGRectMake(275,40,40,40)]; //rightBarButtonItem
}
++index;
}
}
view controller:
- (void)viewDidLoad {
...
UIBarButtonItem *navItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
UIBarButtonItem *navItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:nil];
[self.navigationItem setRightBarButtonItem:navItem1];
[self.navigationItem setLeftBarButtonItem:navItem2];
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, 100.0f);
[self.navigationController.navigationBar setFrame:frame];
...
}
Option 2 (inserting UIBarButtonItem doesn't work):
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,100)];
[self.view addSubview:navigationBar];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(0,25,50,50)];
[navigationBar addSubview:button];
I found the solution of this problem by making adjustment in the Image Edge Insets of the custom button. I had the requirement in the app to increase the height of the navigation bar and after increasing the height makes the rightBarButtonItem and leftBarButtonItem images unpositioned problem.
Find the code below:-
UIImage *image = [[UIImage imageNamed:#"searchbar.png"];
UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchbutton addTarget:self action:#selector(searchBar:) forControlEvents:UIControlEventTouchUpInside];
searchbutton.frame = CGRectMake(0,0,22, 22);
[searchbutton setImage:image forState:UIControlStateNormal];
[searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
// Make BarButton Item
UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
self.navigationItem.rightBarButtonItem = navItem;