UISegmentedControl in the Navigation Bar with the Back button - ios

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.

Related

UICollectionView Top Bar Doesn't Appear In Simulator

I had a little problem which is my uicollectionviewcontroller top bar doesn't appear in simulator and device. I already set the top bar to translucent navigation bar for uicollectionviewcontroller, then I dragged navigation item to navigation bar and finally I set an image for left bar button items to function as back button. Below is there screenshot.
Top Bar Storyboard:
Simulator Result:
It doesn't look like you have a navigation controller, to add a navigation bar programatically without a navigation controller controlling the view hierarchy you can do the following.
-(void) viewWillAppear:(BOOL)animated {
UINavigationBar *navBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[UINavigationBar appearance].barTintColor = [UIColor lightGrayColor];
[self.view addSubview: navBar];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self action:nil];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"Navigation Title"];
navItem.rightBarButtonItem = doneItem;
navItem.leftBarButtonItem = cancelItem;
navBar.items = [NSArray arrayWithObjects: navItem,nil];
[UIBarButtonItem appearance].tintColor = [UIColor blueColor];
}
To add the navigation controller (which will add the bar), highlight the UICollectionViewController in storyboard, and select Editor->Embed in->Navigation Controller from the menus.

Create custom UI bar button (to change view from list view, grid view)

I am trying to create ui bar button like this to change view.
However, I don't know how to do to put 2 ui bar buttom together (1 square button and 4 square button). I only know how to do something like this. How should I do?
It is UISegmentedControl not UIButton
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] init];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"0.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"1.png"] atIndex:1 animated:NO];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[[[self navigationController] navigationItem] setRightBarButtonItem:barButtonItem];

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

How to add Bar Button in navigation bar without navigation controller.

I am new to iOS development.I have create a navigation bar in my iPad application view.I don't need navigation controller that's why i have added only navigation bar. Now i want to add button in that navigation bar.I tried a lot but no success. Is it possible to add only navigation bar with button ? If yes then suggest me some sample code.
"i don't have navigation controller or need it. i just want to add navigation bar in only one view."
Bellow is my code which i write for adding navigation bar in ViewDidLoad()
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1026, 50)];
[navBar setTintColor:[UIColor blackColor]];
[navBar setDelegate:self];
[self.view addSubview:navBar];
Thanks in advance....
I used the Interface Builder to make a static tableView in a UITableViewController.
This UITableViewController is shown modally. Then I added a NavigationBar without the UINavigationController behind it as follows:
//Creating the plain Navigation Bar
UINavigationBar *headerView = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
//The UINavigationItem is neede as a "box" that holds the Buttons or other elements
UINavigationItem *buttonCarrier = [[UINavigationItem alloc]initWithTitle:#"Sign-In"];
//Creating some buttons:
UIBarButtonItem *barBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Zurück" style:UIBarButtonItemStyleDone target:self action:#selector(signInBackPressed:)];
UIBarButtonItem *barDoneButton = [[UIBarButtonItem alloc] initWithTitle:#"Fertig" style:UIBarButtonItemStylePlain target:self action:#selector(signInDonePressed:)];
//Putting the Buttons on the Carrier
[buttonCarrier setLeftBarButtonItem:barBackButton];
[buttonCarrier setRightBarButtonItem:barDoneButton];
//The NavigationBar accepts those "Carrier" (UINavigationItem) inside an Array
NSArray *barItemArray = [[NSArray alloc]initWithObjects:buttonCarrier,nil];
// Attaching the Array to the NavigationBar
[headerView setItems:barItemArray];
// Adding the NavigationBar to the TableView
[self.tableView setTableHeaderView:headerView];
I hope this helps somebody!
UIBarButtonItem *bi1 = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editButton)];
bi1.style = UIBarButtonItemStyleBordered;
bi1.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f];
self.navigationItem.rightBarButtonItem = bi1;
[bi1 release];
You can add buttons to navigation bar as follows:
UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:#"Save"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(save_Clicked:)];
navBar.rightBarButtonItem = btnSave;
[btnSave release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc]
initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(cancel_Clicked:)];
navBar.leftBarButtonItem = btnCancel;
[btnCancel release];

UIPopover: UISegmentedControl instead of title

I have a Popover and the title #"Title". Is it possible to have UISegmentedControl there, instead of the title? In other words, I want a popover which has a segmentedcontrol in the middle of header. How to?
The content of popover is UITableViewController, it is inside a UINavigationController, and I present it via presentPopoverFromBarButtonItem:.
Use a UIToolbar at the top of the popover view. Either in interface builder, or programatically, insert a UISegmentedControl with flexible space either side of it. For example:
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems
[NSArray arrayWithObjects:#"Segment 1", #"Segment 2", nil]];
UIBarButtonItem *segmentedBarButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.toolbar setItems:[NSArray arrayWithObjects:flexibleSpace, segmentedBarButton, flexibleSpace]];
UISegmentedControl *sc = [[[UISegmentedControl alloc] initWithItems:nil] autorelease];
// config the control
UIBarButtonItem *scItem = [[[UIBarButtonItem alloc] initWithCustomView:sc] autorelease];
// add the segmented control, instead of the UILabel title
self.navigationItem.titleView = scItem.customView;

Resources