Trouble adding custom button to navigation controller's toolbar - ios

I have a tableview controller that has a navigation controller. I have added a toolbar to the bottom of the view and now I need to add a custom button. I have the following code (from viewDidLoad), which is producing strange results. When the view loads, I see the custom button for a brief second, then an empty toolbar. At the moment I am only trying to get one custom button to load.
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
[self.navigationController.toolbar sizeToFit];
CGFloat toolbarHeight = [self.navigationController.toolbar frame].size.height + 20;
[self.navigationController.toolbar setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds) - toolbarHeight,
CGRectGetWidth(self.view.bounds),
toolbarHeight)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:[UIImage
imageNamed:#"stub_fav.png"]
style:UIBarButtonItemStylePlain
target:self action:#selector(addFavorite)];
[btn setTintColor:[UIColor whiteColor]];
[btn setImage:[UIImage imageNamed:#"stub_fav.png"]];
NSArray *toolbarItems = [[NSArray alloc] initWithObjects:btn, nil];
[self.navigationController.toolbar setItems:toolbarItems];
Any ideas? Thanks!

Im not sure what btnAddFave is for but it's not the subview of anything. I assume btw and btnAddFave should have the same background image, if so you spelled stub_fav.png wrong. shouldn't it be stub_fave.png

Related

Can't set titleView in Navigation bar center and scope of back button increased

I am trying to push a view controller and set its navigation bar's title but it is not centered due to long title being used i guess. Along with that, scope of the back button is increased till the title view, i.e if I tap with with Milestone's "M", it gets back though it frame is the same.
Bounds of back button are the same but its click impact is elongated.
Below is the code for how I am pushing the view controller.
MilestoneDetailsViewController *controller = [[MilestoneDetailsViewController alloc] initWithNibName:#"MilestoneDetailsViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
and in the MilestoneDetailsViewController, i set the title as following:
self.title = NSLocalizedString(#"Milestone Details", nil);
Back button is picking its size according to the title of previous viewController. you can change it to a empty String for example, in your previous viewController from where you are pushing write this code.
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Try this
UIView *tView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 160 ,44)];
UILabel *bartitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, tView.frame.size.width, 40)];
[bartitleLabel setBackgroundColor:[UIColor clearColor]];
[bartitleLabel setTextAlignment:NSTextAlignmentCenter];
[bartitleLabel setAdjustsFontSizeToFitWidth:YES];
[bartitleLabel setText:NSLocalizedString(#"Milestone Details!",#"")];
[self.navigationItem setTitle:#""];
[tView addSubview:bartitleLabel];
[tView setBackgroundColor:[UIColor clearColor]];
[self.navigationItem setTitleView:tView];
Try like this
UILabel* lbNavTitle = [[UILabel alloc] initWithFrame:CGRectMake(0,40,320,40)];
lbNavTitle.textAlignment = UITextAlignmentCenter;
lbNavTitle.text = NSLocalizedString(#"Milestone Details!",#"");
self.title = lbNavTitle // here self.title is your navigation bar title

Action for UIBarButtonItem on toolbar which is placed in pickerview not working

I have a UIPickerview that is created in interface builder. And via code I placed a toolbar as its subview and a done button as the toolbars bar button item. I want to execute an action when the done button is clicked (hide the pickerview and some extra stuff). But for some reason the action for the done button is not working. Here is the code to create the toolbar and the bar button item in viewDidLoad.
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
toolBar.barTintColor = [UIColor colorWithRed:70/250.0f green:70/250.0f blue:70/250.0f alpha:1.0];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonTapped:)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
toolBar.items = #[flexible, barButtonDone, flexible];
barButtonDone.tintColor=[UIColor colorWithRed:227/255.0f green:178/255.0f blue:49/255.0f alpha:1.0];
[picker addSubview:toolBar];
[picker bringSubviewToFront:toolBar];
And here is the method doneButtonTapped:
-(void)doneButtonTapped:(UIBarButtonItem *)sender{
NSLog(#"Done button tapped");
picker.hidden = true;
[self.view bringSubviewToFront:txtCity];
[self.view bringSubviewToFront:advancedSearchLine];
[self.view bringSubviewToFront:txtCityLine];
}
I checked in layout debugger while running the app and indeed the done button is in the front of the view, so nothing in front of it to prevent it from working. What am I doing wrong?
you cannot add toolbar as subview of picker view.create a container in UIView add toolbar and picker as subview.
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, toolBar.frame.size.height + picker.frame.size.height)];
inputView.backgroundColor = [UIColor clearColor];
[inputView addSubview:picker];
[inputView addSubview:toolBar];

how to show back button on navigation bar?

I was practicing an application with navigation bar. I used UIBarButton to go to the next screen from the main screen.
UIButton *moveLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[moveLeft setTitle:#"Next Screen" forState:UIControlStateNormal];
moveLeft.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12.0f];
[moveLeft.layer setCornerRadius:4.0f];
[moveLeft.layer setBorderWidth:1.0f];
[moveLeft.layer setBorderColor: [[UIColor redColor] CGColor]];
moveLeft.frame=CGRectMake(200, 100.0, 90.0, 30.0);
[moveLeft setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[moveLeft addTarget:self action:#selector(moveToNext) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barbutton= [[UIBarButtonItem alloc] initWithCustomView:moveLeft];
self.navigationItem.rightBarButtonItem = barbutton;
But now when the second screen appears there is no navigation bar.
Here are the screen shots of the app
i tried UIBarButton for back also but still navigation bar is not coming on the second screen.
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
My question how do i show the back button the second screen?? i am not making any custom button but the default is also not coming automatically.
Please guide me.
From your comments, you are presenting the next view controller, instead of this
[self presentViewController:secondScreen animated:YES completion:nil];
Do like this
[self.navigationController pushViewController:secondScreen animated:YES]
Suggestion
Instead of adding subView to the window, you should set the rootViewController of the window which is the recommended way. So change
[self.window addSubview:[navControll view]];
To
self.window.rootViewController = navControll;
No one answered the question correctly, I met the same question, and I have solve it by myself. My solution is delete the navigation controller before the child view controller , than control drag between the tableview controller to the child view controller, select the "show"
You have to use UINavigationController. It will be root view controller for your app. Put mainVC into it and then just pushViewController second vc.
You can access navigation controller via navigationController method of any vc(mainVC in your case).
Simply u can add custom back button to navigationBar
UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, button_width, button_height)];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

dismissing a UIPickerView once the user selected all rows

I have a UIPickerView with 2 components where the user can select a font and a font size.
I want to dismiss the view once the user has selected both.
Can I somehow do this using the didSelectRow:inComponent method?
Yes, if you record the component that is provided to the method then when all of your components have had a selection made you can dismiss.
Note that users may not like it very much and adding a toolbar above the picker with a done button might work better.
I would suggest creating a container view to hold the picker and the toolbar (subviews), then show / hide the container view with whatever animations you like (instead if where you currently show the picker view). The picker and the toolbar don't need to be linked in any way, just laid out nicely in the container.
I hope it will help you
UIActionSheet *action = [[UIActionSheet alloc] init];
action.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
UIPickerView *pickerFromage=[[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 0 , 0)];
UIToolbar *pickerAgeToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerAgeToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerAgeToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(PickerDoneClick)];
[doneBtn setTintColor:[UIColor blackColor]];
[barItems addObject:doneBtn];
[pickerAgeToolbar setItems:barItems animated:YES];
pickerFromage.delegate=self;
pickerFromage.dataSource=self;
pickerFromage.tag=value;
pickerFromage.showsSelectionIndicator=YES;
[action addSubview:pickerAgeToolbar];
[action addSubview:pickerFromage];
[action showInView:self.view];
[action setBounds:CGRectMake(0,0, 320, 464)];`
-(void)PickerDoneClick{
[action dismissWithClickedButtonIndex:0 animated:YES];
action=nil;
}
You can do it this way:
Take a UIView in xib and then add a UIPickerView and a toolbar with UIButton on it. Give the delegate to the UIPickerView and assign an action to the UIButton. Take an outlet of UIView and in your implementation file(ie. .m file). Hide the UIView and assign the frame to the bottom of the screen. Lets say you have named the view as myPickerView.
myPickerView.frame = CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(myPickerView.frame),
CGRectGetHeight(myPickerView.frame));
myPickerView.hidden = YES;
After doing this you can animate the view from bottom to upwards by setting the frame again in animation block, and can also unhide it.
When you press done, again animate the view and set the frame as above.
Below is the code for animating
- (IBAction)showPicker:(UIButton *)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(0,
100,
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
action for done button:
-(IBAction)doneClicked:(id)sender {
self.myPickerView.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
[self.myPickerView setFrame:CGRectMake(CGRectGetMinX(self.view.bounds),
CGRectGetMaxY(self.view.bounds),
CGRectGetWidth(self.myPickerView.frame),
CGRectGetHeight(self.myPickerView.frame))];
}];
}
Hope this helps.

UIToolbar not showing UIBarButtonItem

I'm using a storyboard and I've a split view where the master is a UITableViewController. Like the iPad Mail app, I'd like to display a UIToolbar.
I wasn't able to add the toolbar via storyboard but I managed to add it programmatically. I'm also able to add a UILabel to the toolbar, but I can't find a way to add a refresh button or any kind of UIBarButtonItem.
Any idea?
- (void)viewDidLoad {
[super viewDidLoad];
[self.navigationController setToolbarHidden:NO];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 0.0f, 80.0f, 40.0f)];
label.text = #"last updated...";
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont systemFontOfSize:13.0];
[self.navigationController.toolbar addSubview:label];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStylePlain target:self action:#selector(action:)];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:#"Item1" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *buttons = #[item1, item2, nil];
[self.navigationController.toolbar setItems:buttons animated:NO];
Found the answer thanks to the Apple iOS Forum!
When you use the toolbar of the navigation controller, you have to set the toolbar buttons on the active view controller's toolbarItems property, not on the actual navigation controller's toolbar itself.
From the UINavigationController docs:
Displaying a Toolbar
A navigation controller object manages an optional toolbar in its view
hierarchy. When displayed, this toolbar obtains its current set of
items from the toolbarItems property of the active view controller.
When the active view controller changes, the navigation controller
updates the toolbar items to match the new view controller, animating
the new items into position when appropriate.
For example:
[self setToolbarItems:buttons animated:NO];

Resources