This may not be possible, but I'm hoping someone will have an idea how to do it.
I have an app I'm porting from iPhone only to Universal. On the iPhone, I'm using a Tabbed application. I use three tabs for the normal data to be displayed. I have a forth tab that's only displayed if certain conditions are met. To add the tab, I do:
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
UITabBarController *tabController = (UITabBarController *) self.rootViewController;
NSMutableArray* newArray = [NSMutableArray arrayWithArray: tabController.viewControllers];
[newArray addObject: [theStoryboard instantiateViewControllerWithIdentifier: #"AdditionalView-Phone"]];
[tabController setViewControllers:newArray animated:YES];
}
For the iPad, I have enough space on the initial view to display everything from the main three tabs in the iPhone UI. So all I need is one additional (small) view for the "Additional" data. I wanted to do it using a popOver view, so I set up the initial view with a Nav bar and popover button as in the Utility App template. But now I'm stuck. I can't figure out how to create that popover button at run time and make it do the segue to the popOver view properly. I can add the button like this:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc] initWithTitle: #"Modem" style: UIBarButtonItemStylePlain target: self action: #selector(togglePopover:)];
self.navBar.topItem.rightBarButtonItem = flipButton;
but I get an exception: 'NSInternalInconsistencyException', reason: 'UIStoryboardPopoverSegue must be presented from a bar button item or a view.' I'm pretty sure this is because I don't have an anchor set for the popOver segue. The button doesn't exist in the storyboard, so I can't set it there. And I can't seem to find an API to set it at run time.
I also tried creating the button in IB, but not in the view hierarchy, and then just setting the rightBarButtonItem property to my existing button. That also works, but I still can't set that button as the anchor for the popover view. I can set the Navigation Bar as the anchor, but that makes it anchor to the title in the nav bar, which looks silly.
Any ideas?
I had the same problem and solved it by creating a UIBarButtonItem in the Storyboard for the view controller but not part of the view hierarchy.
In IB, Drag a bar button item to the dark bar below the view controller view, drop it next to the "First Responder" and "View Controller" icons. Create a (strong) IBOutlet for it. Then create a popover segue from it to the destination view controller by dragging from the bar button item to the destination. It seems like this is the only way to set it as the anchor. Choosing it as the anchor for an existing segue does not work (looks like an IB bug).
In viewDidLoad you can assign this bar button item to the navigationItem (or where ever you like) and the segue works as expected.
I was curious about this too so I made a quick test project. You're right, there doesn't seem to be a way to configure the popover segue at runtime or add an anchor point to a button that's not in the view hierarchy using Interface Builder.
My solution was to set everything up in IB with the UIBarButtonItem visible and connected to an IBOutlet property, then remove it from the navigation bar in -viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = nil;
}
Then I simply add it back or remove it by tapping another button:
- (IBAction)toggleBarButtonItem:(id)sender
{
UIBarButtonItem *item = (self.navigationItem.rightBarButtonItem == nil) ? self.popoverBarButtonItem : nil;
[self.navigationItem setRightBarButtonItem:item animated:YES];
}
You could conditionally keep or remove the button in -viewDidLoad the same way. The segue remains anchored to the UIBarButtonItem.
I'm gonna try making a dummy view that's the size and shape of the views I want to present the popover from, wire that to the segue popover target, and then move the view to the right position in prepareForSegue:sender:
I'm not sure this is exactly what you want, but this is what I would do. Create a button and set it up with some target/action. Call that target/action method
presentPopover:(UIButton *)sender;
Then in the presentPopover method, say
UIViewController *customAdditionalViewController = [[MySpecialVC alloc] init];
//Configure the customAdditionalViewController
//Present the customAdditionalViewController in a Popover
UIPopoverController *popover = [[UIPopoverController alloc] initWithViewController:customAdditionalViewController];
//Set popover configurations
[popover presentPopoverFromRect:sender.frame inView:self.view permittedArrowDirections:/*whatever you want*/ animated:YES];
That is how I would handle your use case.
Related
I have an app that uses a side menu and has a few main screens that can be accessed from the menu and others that can only be accessed from these screens.
What I want is to have a menu button on the navigation bar that opens the menu and can only be visible on the main screens. On the other screens I want to have a back button, instead of the menu button.
I've already put the menu button like this:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
But I can't figure out how to change it with the back button when I need it.
Assuming than by "main screens" you mean root (first) view controllers in the navigation view controllers corresponding to the selected side menu items, this might be a solution for your problem: you can create a superclass for all your view controllers, say MyBaseViewController and rewrite viewWillAppear: method, that will determine whether it should have a default back button or a "revealSideMenu" button, based on whether it's a "main screen" or not.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self == [self.navigationController.viewControllers firstObject]) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconMenu"] style:UIBarButtonItemStylePlain target:self.revealViewController action:#selector(revealToggle:)];
}
}
you can do that in 2 different ways:
the first is in the viewDidAppear check if the back button is already present (when you push a VC the back button automatically added but is avalaible programmatically only after the viewDidAppear) and then decide to add or not the menu button,
the second is add a parameter to your VC init method like isRoot or hasMenu or whatever you like to name it, and using that flag decide to add the menu or the back button
if you chose to add your own back button you have to call this method in your back selector
[self.navigationController popViewControllerAnimated:YES];
The easiest way, IMO, is just to click on the title bar of the first ViewController and in the Attribute Inspector (⌥+⌘+4) change the Navigation Item info the way you want: Title -> what will show up in the back button* or if you want it to say something other than the Title of the first ViewController or the word "Back" you can just put it in the Back Button field.
*If the Title of the second ViewController is too long for it to fit it will be replaced with the word "Back" (and if the word "Back" doesn't fit it will only have the arrow sign).
I have following view controllers hierarchy:
View Controller VC1
Tab Bar Controller TBC1 - configured in storyboard to lead to a Table View Controller TVC1 and a Map View Controller MVC1
Table View Controller TVC1
Table View Controller TVC2
In VC1, I do this:
[self.navigationController pushViewController:TBC1 animated:YES];
This rightly brings up tab bar controller, with TVC1 in focus.
TVC1 shows back button in its navigation bar (programmatically created from VC1 code), which will get me to VC1, which is expected.
However, from TVC1 onwards, I need one more navigation to TVC2. I am trying to add right button to the TVC1 navigation bar for this, but it doesn't show up.
Here is the code I use in TVC1 (rightButton is UIButton type property of TVC1):
self.rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
target: self
action: #selector(MySelector:)];
self.rightButton.style = UIBarButtonItemStyleBordered;
self.rightButton.title = #"";
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: self.rightButton, nil];
(specified blank title and style just to ensure if that's the issue which is causing this, I don't actually need those values)
MySelector is declared in TVC1.h as:
- (void) MySelector:(id)sender;
And it is properly implemented, too.
But rightButton above does not display in TVC1 navigation bar.
What am I missing?
I suspect its with TBC1 (tab bar) that comes between VC1 and TVC1, and somehow it resets navigation properties.
But then I argue that I see navigation bar on TVC1, and a left button leading to VC1.
I checked that in TBC1, self.navigationItem.rightBarButtonItems has 1 object inside which is definitely the rightButton I am adding.
Where am I wrong?
Note: Above is found in all of iOS 5.0, 5.1 and 6.0 simulators.
It seems to me that your are missing UINavigationController between TVC1 and TVC2 in your storyboard. If you are using storyboards then you can create navigation item Add button type on the navigation controller itself and have a PUSH segue to TVC2. See this diagram if that makes sense. If this doesn't solve your problem then please upload example code and I will have a look.
[EDIT]
I had reproduced your issue by creating your view controllers structure in storyboard.
If you notice here TVC1 doesn't have it's UINavigationController but it is inheriting it from VC1. Solution to your problem is rather than adding rightButton onto self add it to self.parentViewController and you will see rightButton in TVC1. But mind you it will also appear in MVC1 as it is belong to TBC1's parent. You can hide right bar button in MVC1's viewWillAppear if you don't want it there. Following is the code.
self.rightButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem: UIBarButtonSystemItemAdd
target: self
action: #selector(MySelector:)];
self.rightButton.style = UIBarButtonItemStyleBordered;
self.parentViewController.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: self.rightButton,nil];
If you want to Add right button into the TVC1's navigation controller then you need Embed TVC1 into UINavigationController. To do this, select TVC1 screen in the storyboard -> Editor -> Embed In->Navigation Controller. When you do this your code will also work and will show you right button but you will have two navigation controllers(see image below) in it because of your structure of storyboard. You will need to hide Parent's navigation controller in to the TVC1's view did load and have left button to Pop to Parentview Controller. You do the same in MVC1.
Hope this helps! Happy coding :)
Had the same problem recently in Swift and found that embedding the child view in a navigationController was still the correct way to be able to access the rightBarButtonItems.
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.)
I am implementing a WEPopoverController and would like to set the title and put a button on top of the popover. Is it possible to do that with this controller?
This is what I have so far. I am loading TableViewController into the popup.
I tried to set self.title=#"title"; in the tableviewcontroller's viewdidload but that didn't help. I only see the tableview with borders inside.
I tried to create a tableview controller in storyboard and programmatically load into the popup but i couldn't resize it. Plus i am not sure if that would be good programming practice.
This is how i load the popuptableview. Would it be easier to use a UIViewController? I don't really need the tableview.
PopUpTableViewController *popUpTable = [[PopUpTableViewController alloc]initWithStyle:UITableViewStylePlain];
// TestViewController *testView = [[TestViewController alloc]init];
self.popoverSettingsController = [[WEPopoverController alloc]initWithContentViewController:popUpTable];
[self.popoverSettingsController presentPopoverFromRect:frame
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionDown|UIPopoverArrowDirectionUp
animated:YES];
I looked through the preference options but didn't see anything about title. Would i have to resize the container?
You should create a navigation controller with your popUpTable as root view controller and then use the navigation controller as the content view controller for the pop over.
If the above is done, then your logic of self.title inside popUpTable and creating bar button item will work!
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.