UIToolbar not showing UIBarButtonItem - ios

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

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

Do I need to embed a UINavigationController & Programmatically add UINavigationBar?

I don't see all of my navigation bar.
There is only a outline of where a nav bar would be.
I set the navigationBar Color and tint colors so as to define this nav bar even if it had no items.
I used Storyboards to make the currently functional UI.
My Story Board UI includes Delegates,Custom Collection View Cells,Custom Table View Cells,And Animations.
By the end of my app I wanted to add nav bars programmatically to each of my ViewControllers. (see code)
I have used this code in 6 other classes. And I Have 6 Perfectly Flawless navigation bars in those ViewControllers.
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizeralloc]
initWithTarget:self
action:#selector(titleGestureLabelMethod)];
UILabel * titleView = [UILabel new];
titleView.text = #"Title";
titleView.textColor = [UIColor greenColor];
[titleView sizeToFit];
titleView.userInteractionEnabled = YES;
[titleView addGestureRecognizer:tapGesture];
self.navigationItem.titleView = titleView;
[self.view addSubview:titleView];
[self.navigationController.navigationBar setTranslucent:NO];
UIBarButtonItem *lButton = [[UIBarButtonItem alloc] initWithTitle:#"L"
style:UIBarButtonItemStylePlain
target:sel
action:#selector(lButtonMethod)];
[self.navigationItem setLeftBarButtonItem:lButton animated:YES];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithTitle:#"R"
style:UIBarButtonItemStylePlain
target:self
action:#selector(rButtonMethod)];
[self.navigationItem setRightBarButtonItem:rButton animated:YES];
self.navigationController.navigationBar.tintColor = [UIColor redColor];
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.view addSubview:testTwoNavBar];
}
You need to embed your view controller into a navigation controller. If you're using Interface Builder, from your storyboard select the view controller. In the menu select editor and choose embed in navigation controller. It's hard to understand what you really need though from this question.

Trouble adding custom button to navigation controller's toolbar

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

View in popover is missing some, but not all, subviews

I am trying to make a view reveal inside a popover. Something I have done numerous times. However, for some reason this time, the popover is appearing but missing some (but not all) of it subviews.
More specifically, this view contains a label, a text field, a button, and also has a navigation bar at the top. When revealed, it displays a label, but not the text field, or button. Ive tried with and without a XIB and results are the same.
Here is the declaration of the popover.. (triggers on button click)
if (popover)
{
[popover dismissPopoverAnimated:NO];
popover = nil;
}
// create the settings view controller and display it in a popover
LoginViewController *loginViewController = [[LoginViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginViewController];
// Initialize the popover, set the parent view as the delegate and allocate the popover's origin on the screen
popover = [[UIPopoverController alloc] initWithContentViewController:nav];
popover.delegate = self;
[popover presentPopoverFromRect:button.frame inView:button.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
and in the view did load of the loginViewController I have
self.navigationItem.title = NSLocalizedString(#"POPOVER_TITLE_INFORMATION", nil);
// setup navigation buttons
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"BUTTON_TITLE_CANCEL", nil) style:UIBarButtonItemStyleBordered target:self action:#selector(cancel)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"BUTTON_TITLE_SEARCH", nil) style:UIBarButtonItemStyleDone target:self action:#selector(search)];
self.navigationItem.rightBarButtonItem = searchButton;
self.navigationItem.leftBarButtonItem = cancelButton;
[_loginTextLabel setText:NSLocalizedString(#"DESCRIPTION_TEXT", nil)];
[_loginTextLabel setNumberOfLines:0];
_loginTextLabel.frame = CGRectMake(20, 30, 728, 40);
[self.view addSubview:_loginTextLabel];
_loginTextField.frame = CGRectMake(20, 80, 264, 30);
[self.view addSubview:_loginTextField];
[_loginSubmitButton setTitle:NSLocalizedString(#"BUTTON_TITLE_SUBMIT", nil) forState:UIControlStateNormal];
[_loginSubmitButton addTarget:self action:#selector(submitBtnClk:) forControlEvents:UIControlEventTouchUpInside];
[_loginSubmitButton setFrame:CGRectMake(20, 130, 208, 44)];
[self.view addSubview:_loginSubmitButton];
Nothing overly complex, however the label is the only thing that actually displays to the screen.
I found the solution.
It would seem that Popovers AutoResize their components, and the widths of the button and the textfield were not large enough. Essentially, the popover screen is smaller than a normal view covering the full window, so it was shrinking the widths of the textField and button all the way to 0 when i guess the popover was adjusting the view.
So basically, I had to jack up the widths of those subviews to compensate.

UISegmentedControl in the Navigation Bar with the Back button

I'm adding a UISegmentedControl to the Navigation bar programatically where the titleView should be. But as Apple docs have mentioned under titleView, This property is ignored if leftBarButtonItem is not nil.
But I want to have the back button as well. Like they have illustrated in their own images!
Below is the code I add the UISegmentedControl.
self.navigationItem.leftBarButtonItem = nil;
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Filter_Personnal", #"Filter_Department", #"Filter_Company", nil];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
self.navigationItem.titleView = statFilter;
Is there another way to add a UISegmentedControl along with the Back button as well?
Thank you.
Try this
Remove this line --- > self.navigationItem.leftBarButtonItem = nil;
Add this instead
UISegmentedControl *statFilter = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Filter_Personnal", #"Filter_Department", #"Filter_Company", nil]];
[statFilter setSegmentedControlStyle:UISegmentedControlStyleBar];
[statFilter sizeToFit];
self.navigationItem.titleView = statFilter;
Only change is I have added this line :
[statFilter sizeToFit];
Hope this Helps !!!
You can create a UIBarButtonItem with a custom view which could potentially be your UISegmentedControl.
Something along the lines of the following may work.
//create segmented control with items
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"One", #"Two", nil]];
//create bar button item with segmented control as custom view
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
//add segmented control bar button item to navigation bar
[[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];
I haven't tested this but it should be along the right lines of what you need.

Resources