UIPopover: UISegmentedControl instead of title - ios

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;

Related

Keyboard toolbar for multiple textFields

In my project, some viewControllers have multiple text fields,
I found how to add a toolbar above keyboard with a "Ok" button to hide keyboard when the button is tapped.
The code I am using is this below :
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:_destField action:#selector(resignFirstResponder)];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 34)];
toolbar.items = [NSArray arrayWithObjects:flex, barButton, nil];
_destField.inputAccessoryView = toolbar;
How can I easily reuse this code in the same view controller?
The "target" makes this difficult, is there a way without creating a toolbar for each textField?
Thanks!
If this code is in a view controller then there is a simple solution. Change the barButton to something like this:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissKeyboard)];
Then add this method to the view controller:
- (void)dismissKeyboard {
[self.view endEditing:YES];
}
That will dismiss the keyboard no matter what view is showing it.
Now you can reuse that toolbar as the inputAccessoryView for any text field/view in the view controller.

How do I create a keyboard accessory view with Auto Layout in Interface Builder?

I want to add a "Done" button and a segmented control to a Decimal Pad keyboard. Ideally I would like to lay out a keyboard accessory view with Auto Layout in Interface Builder.
Is this even possible? Do I create a new XIB or can I do it in the existing Storyboard scene somehow? How do I attach the accessory view to the appropriate text field?
Would you mind doing it programmatically?
Typically you take a UIToolbar to a UITextField with your items, but you may need to subview the UISegmentedControl;
UIToolbar *keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithFrame:...
// Customize segmentedControl's properties here.
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(doneButtonPressed)];
[keyboardToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
[keyboardToolbar addSubview:segmentedControl];
[textField setInputAccessoryView:keyboardToolbar];
EDIT: It is possible to create the toolbar initially in IB, but you have to drag it off the view and to the left hand sidebar containing the scene and link it to a reference outlet, then assign it with setInputAccessoryView in your viewDidLoad.

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.

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

How to add buttons to the top bar of a detail view from a UITableView

When an item from a UITableView is selected, a detail view is loaded which has a banner bar at the top with a back button on it to navigate back to the table.
How do I add other buttons to that banner bar?
As Andrew said, you can add custom views to the navigation bar. For example, if you are looking to add multiple buttons to the right side of the navigation bar, you can do something like this:
// right side of nav bar
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 106, 44)];
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelAction:)];
cancelButton.style = UIBarButtonItemStylePlain;
[buttons addObject:cancelButton];
[cancelButton release];
[toolbar setItems:buttons animated:NO];
toolbar.barStyle = -1;
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Make sure to adjust the width (106 above) if you need more or wider buttons, and then provide methods for the selectors (deleteAction: and cancelAction: above).
From your description, it sounds like you have a UINavigationController correctly set up. That "banner bar" you are describing is properly called a navigation bar.
From within the detail view, you can use:
UIBarButtonItem* button = [[[UIBarButtonItem alloc] initWithTitle:"HiMom" style:UIBarButtonItemStylePlain target:self action:#selector(onHiMom:)] autorelease];
self.navigationItem.rightBarButtonItem = button;
You can also add custom views (instead of a button) plus a few other settings. I suggest poking around the documentation for UINavigationItem, UINavigationBar, and UIBarButtonItem for ideas.

Resources