UINavigationBar with a 'Back' button. Create with XIB - ios

My question is next:
In interface builder i create UINavigationBar and I want to create 'Back' button item, but I dont see any button.
I use this code:
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back";
mynavBar.backItem.backBarButtonItem = myBarButtonItem;
mynavBar - this is my IBOutlet.
Thanks for help!

You can use a navigation bar as a standalone control or in conjunction with a navigation controller. When you use a navigation bar as a standalone control you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed.
So in your case you would use something like this:
UIBarButtonItem *myBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
myBarButtonItem.title = #"Back";
UINavigationItem *right = [[[UINavigationItem alloc] initWithTitle:#"Hello!"] autorelease];
right.leftBarButtonItem = myBarButtonItem;
[mynavBar pushNavigationItem:right animated:YES];
You may want to look into using UINavigationViewController though.

If you want a custom button on the left, use mynavBar.leftBarButtonItem instead of backItem.
The backItem will only be visible, after you presented another viewcontroller via pushViewController:. (If you didn't set you own backbutton, the default backButton with the title of the previous viewController will be created automatically.)
//edit: perhaps you look for that:
Draw custom Back button on iPhone Navigation Bar

I think this is the actual way apple want this to be implemented.
Put UINavigationBar
Set outlet to the UINavigationItem
This is the catch
Override navigationItem property to return the UINavigationItem you created.
That's it.
-(UINavigationItem *) navigationItem
{
return self.navigationItem1;
}
If your navigationItem is still in the UINavigationBar, I think you will need to have a strong outlet to the UINavigation Bar too. Please correct me if I am wrong here.

Related

How to add Text Field and Images in Navigation Bar of Tab Bar?

I want to add text field and search icon on navigation bar of tab-bar controller. Please check attach image.
Note: This is home screen and if user click on any menu item it will goes to detailvc so i add one navigation controller on homevc and hide it.
I want to show search icon and text-field at navigation-bar of tabbar. how can i do this ?
set textfield with left view in navigation title view
navigationItem.titleView = UITextField()
Subclass the navigationbar
#interface SearchNavigationBar : UINavigationBar
-(void)layoutSubviews
{
[super layoutSubviews];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"searchImage"] style:UIBarButtonItemStylePlain target:self action:#selector(searchPrompt:)];
self.topItem.rightBarButtonItem = buttonItem;
}
Add it as the class of your navigation controller
and get the object this way
UINavigationController *navBar = [[UINavigationController alloc] initWithNavigationBarClass:[SwitchAssessmentNavigationBar class] toolbarClass:nil];
and then you can call custom methods on the tool bar such as delegates that trigger events in your main page to change
searchBar in center of navigationBar:
self.navigationItem.titleView = self.searchBarTop;
searchBar in left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
Create an UIView custom class which will hold your custom search bar.
Then add that view's instance into your navigationController's view.
[self.navigationController.view addSubview:yourCustomView];
I have to add on own custom custom class of UItabbar-Contrller

How do I add a back button to my UINavigationBar?

A user arrives at this viewController from the previous one through a push segue, so I want there to be a back button in the UINavigationBar to allow them to return.
Normally this back button would appear by default if I right clicked on the viewController in storyboard and selected Embed in > Navigation Controller, but doing this is causing crashes, and I prefer doing things programmatically, so I decided to do it in viewDidLoad like so:
// Nav bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
// Back Button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style: UIBarButtonItemStyleBordered target:self action:#selector(Back)];
self.navigationItem.leftBarButtonItem = backButton;
[self.view addSubview:navbar];
This successfuly adds a navigation bar up top, but it doesn't add the back button as expected. I've tried the solutions given here, here, and here, but none have solved the problem.
You have constructed a UINavigationBar and a UIBarButtonItem but never connected the two.
UINavigationController manages a UINavigationBar for you and sets it's topItem to be the navigationItem of the topmost view controller in the navigation stack. If you need to build your own navigation bar then you need to manage it's item stack yourself. Take a look at UINavigationBar's - pushNavigationItem:animated: method.
However given that you seem to want the look and functionality of a UINavigationController it is unclear to me why you are adding a UINavigationBar view yourself instead of using that container view controller.

Is it possible to show a real Popover instead of default slide-out menu when using a UISplitViewController?

I am currently working with a UISplitViewController and instead of having this default slide-out-menu, i want a real UIPopover that appears if i click the UIBarButtonItem. What do i have to do and is there an easy way of configuring this ?
You first need to overwrite the left bar button item, with the button that can be used to display the popover.
Use the following -
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithTitle:#"Popover" style:UIBarButtonItemStylePlain target:self action:#selector(presentPopover:)];
self.navigationItem.hidesBackButton = NO;
self.navigationItem.leftBarButtonItem = nil;
Now you can use the target added, and then perform the associated function, as per your choice.
-(IBAction)presentPopover:(id)sender
{
// Perform your operations
}
Then just use a popover view controller instead with the same content you would have used in the splitview pane that slides out.

How do I add a UISegmentedControl to my UIToolbar using Storyboard?

I'm trying to add a Segmented Control to my UIToolbar below, but when I try to drag it over in Storyboard it replaces my Table View.
Also, when I try to add UIBarButton to my Toolbar it pushes my Prototype Cells down... do I have my views hierarchy wrong?
When I add a UIBarButton item anyway, in the simulator the toolbar is not Black Transparent as I set in my navigation control inspector. Whats up with that?
thanks!
To create Segmented Control element in the toolbar with Interface builder it is possible to use the following hint
1) Create Segmented Control Bar Button Item in Navigation Controller:
2) Drag Bar Button Item up to Navigation Item
3) Then you should get the following structure
4) Now you can select Segmented Control to set its properties
1 - Do it programmatically. You need to assign a frame to your UIToolbar first, then you can add elements in it (the segmentedControl). I suggest though to use UIBarButtonItems, which basically are the same.
To stick it to the bottom, try to do this way:
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]
CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:frame];
toolBar.frame = CGRectMake(0,self.view.frame.size.height-toolBar.frame.size.height,SCREEN_FRAME.size.width,toolBar.frame.size.height);
//Setting up the items
UIBarButtonItem *first = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
UIBarButtonItem *second = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
//Creating an array with the items
NSArray *items = [NSArray arrayWithObjects:first,second, nil];
//Assigning the array to the toolBar
[toolBar setItems: items];
Then you can set its style with
mytoolbar.barStyle = UIBarStyleBlack;
2 - Same as above, framing.
3 - You need to set your navigationController style to Black.
Use self.navigationController.navigationBar.style = UIBarStyleBlack; ,or in IB choose the buttons and from Inspector set Tint or Style to whatever you want.
Check also this question
I presume there's a navigation view controller somewhere in here. They have their own toolbar, but it's hidden and empty. The toolbar you're seeing is just a simulation of a toolbar for the purpose of letting you edit the UI.
So to do this with storyboard, don't add the segmented control to the simulated toolbar, create a new toolbar and add it to the tree of views available in your controller (as a sibling to your Table View) and create an IBOutlet to reference it. Then in your viewDidLoad method assign the items in the toolbar to the toolbar already created by the view controller.
In this example I've created a property on my view controller called (cunningly) toolbar:
#property (nonatomic, retain) IBOutlet UIToolbar toolbar;
In my view controller's implementation I assign the toolbar items manually:
-(void)viewDidAppear:(BOOL)animated
{
[self setToolbarItems:self.toolbar.items animated:NO];
[self.navigationController setToolbarHidden:NO animated:YES];
}
You'll need to hide the toolbar when the view controller is popped, for instance in the calling view controller:
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
Assuming you want to hide it, of course.
I did this by placing the UISegmentedController in a Bar Button Item.
Add a toolbar to the UINavigationController window, then enable the display of this toolbar on a ViewController screen.
Drag a bar button into the toolbar on the ViewController screen, and drag a UISegmentedController into that (resizing the button to the full width of the screen)
To wire it all up, ctrl-drag from the UISegmentedController to the corresponding .h file, drag from "referencing outlet" to the yellow view controller icon (using the same variable name), then drag from "ValueChanged" to the yellow icon (selecting the method on the controller that you want to call on a new selection).
This all seems to work as expected. (If you can't select a segment on the controller to set an initial value, you didn't connect up the referencing outlet as described above. That stumped me for a little while.)

ios force back button style w/ no navigation controller

I'm not using a navigation controller so I'm just getting a plain rounded rect. I want to get the arrow-esque back button instead, is that possible?
From your comments, you would like to use a navigation controller but don't have room for the navigation bar on your root view controller.
self.navigationController.navigationBarHidden
Will hide or show the navigation bar within a view controller. That should make your life simpler.
It is rather simple, create your custom back button like so:
UIButton *button = /* your custom button, most likely with an image */
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItem:barButtonItem];

Resources