Resize UITableViewController for display in a popover - ios

I have a UITableViewController that I am trying to display in a UIPopoverController. No matter what do, the tableview is always displayed at full size within the popover.
In my StoryBoard I've set the UITableViewController's Size to Freeform. I've explicitly set the UITableView's height and width in the Size inspector.
Here is how I setup the controller and popover:
UIStoryboard* sb = [UIStoryboard storyboardWithName:#"MainStoryboard-iPad" bundle:nil];
self.settingsController = [sb instantiateViewControllerWithIdentifier:#"SettingsViewControllerID"];
self.settingsPopover = [[UIPopoverController alloc] initWithContentViewController:_settingsController];
I've also tried setting the controller's popover size explicitly:
self.settingsController.contentSizeForViewInPopover = CGSizeMake(400.0, 400.0);
When I run the app and activate the popover - it is almost as tall as the main screen.
Am I missing something obvious here?
Thanks in advance,
CS

You can change the tableView size by using below code.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIStoryboardPopoverSegue *popoverSegue;
popoverSegue = (UIStoryboardPopoverSegue *)segue;
UIPopoverController *popOverController = popoverSegue.popoverController;
[_popOverController setPopoverContentSize:CGSizeMake(width, height)];
}

The UITableViewController always wants to be the full size of the window, or popover (which is a special kind of window). If you want a smaller table view, use a UIViewController instead as the content controller of your popover. You should then be able to resize it in IB.

Related

JASidePanels fix right panel bounds

I am using JASidePanels to show a viewController on the right side of the screen.
The controllers are loaded from the Main storyboard in this manner from a JASidePanelController subclass:
-(void)awakeFromNib{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
TRMPracticeViewController *practiceVC = [mainStoryboard instantiateViewControllerWithIdentifier:#"Practice"];
TRMPracticeSetupViewController *practiceSetup = [mainStoryboard instantiateViewControllerWithIdentifier:#"PracticeSetupVC"];
[self setCenterPanel:practiceVC];
[self setRightPanel:practiceSetup];
[self setRightFixedWidth:practiceVC.view.frame.size.width/3.0f];
}
The TRMPracticeSetupViewController has a fixed frame in the storyboard of 200px x 320 px and I would like to maintain the width once the controller is loaded and added to the side (or at least a width proportional to the size of the screen).
However, once the vc has been loaded and added, its frame is set equal to the center view controller, which has of course the size of the screen. So, the views inside the right vc, go under the center vc because of the auto layout constraints that fix their margin to the container bounds.
How can I fix the right panel bounds width to e.g. 200px ?
JASidePanelController has a method for setting fixed width of either left or right panel. Just add this to your code:
self.rightFixedWidth = 200.0f;

UIView in xib displays as portrait when opened in landscape

I just added a separate view controller and xib in a project that uses storyboards throughout. I display it from inside another view controller with the usual code:
OtherVC *othervc = [[OtherVC alloc] initWithNibName:nil bundle:nil];
[self.view addSubView: othervc.view];
In portrait orientation, he xib displays and auto rotates properly right out of the box.
But when I display it when the iPad is already in landscape, the size of the view is portrait and positioned off to the left.
What's going on? There doesn't seem to be a way to add constraints to the view.
Thanks!
This is not the right way to present a separate viewController.
The problem is that you are adding a view on the actual view..but this view have a certain frame that will remain the same when you add it on the view..in both case (landscape and protrait). So, to understand, you should check the orientation and then set the frame of the view properly. But as i said, the way that you are using is wrong.
In your case you should use segue from storyboard.
Or, at max, by code but with something like:
OtherVC *othervc = [self.storyboard instantiateViewControllerWithIdentifier:#"Identifier"];
[self presentViewController:othervc animated:YES completion:nil];
where Identifier is set in your viewController in the storyboard by the inspector panel at the right.
Or, if you instead wanted to say "NIB" you should to do:
OtherVC *othervc = [[OtherVC alloc] initWithNibName:#"yourNibName" bundle:[NSBundle mainBundle]]; //Or your correct bundle but i guess will be this.
[self presentViewController:othervc animated:YES completion:nil];
So after this, if you are sure to have set both orientation on your project, your view will be in landscape.
Now you will use AutoLayout or normal mask rules without AutoLayout to manage the elements on the view.

Add a UINavigationController nested inside a container view controller to a UITabBarController

I have a UIViewController (red) set as the first tab of a UITabBarController as shown in the storyboard below. This view controller is a container view controller and loads a UINavigationController inside its contentView (the white rectangle inside the red view controller).
This is my code for loading the navigation controller inside the red view controller's contentView:
- (void)viewDidLoad
{
[super viewDidLoad];
// instantiate navigation controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *navigationVC = [storyboard instantiateViewControllerWithIdentifier:#"N"];
// place navigation controller inside content view
[self addChildViewController:navigationVC];
navigationVC.view.frame = self.containerView.bounds;
[self.containerView addSubview:navigationVC.view];
[navigationVC didMoveToParentViewController:self];
}
From what I know about view controller containment this should work as I am explicitly setting the frame for the navigation controller. However, when there are enough cells in the tableView to exceed the container's height there is always a bar at the end of the tableView when I scroll down. I have set the tableView's backgroundColor to orange and the cell's backgroundColor to white in order to see the difference.
How do I get rid of that orange gap at the end of the tableView?
(Note: I am not using autolayout and I need a solution that works for both - iOS7 and iOS6.)
I know you are also looking for an answer which works on iOS 6, but on iOS 7 and above you can use
self.extendedLayoutIncludesOpaqueBars = YES;
Have you tried setting self.edgesForExtendedLayout = UIRectEdgeAll; in -(void)viewDidLoad of Table View Controller - Root?
Note: iOS 7 only

UIView in UITabBar don't extend to full

I have a UIViewController called DashBoardViewController that acts as delegate for a UITabBar. In its xib I have placed a UITabBar with 3 UITabBarItem.
Each of these items activate a different View Controller, let's call them ViewController1, ViewController2, ViewController3
DashBoardViewController is supposed to show ViewController1 and select the first bar on loading, so in my initWithNibName I have what follows:
...
ViewController1* vc = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
[self.view addSubview:vc.view];
self.currentViewController = vc;
...
I implement the UITabBarDelegate having something as follows:
if (item == viewController1Item) {
ViewController2 *vc2 = [self.childrenControllers objectAtIndex:1];
[self.currentViewController.view removeFromSuperview];
[self.view addSubview:vc2.view];
self.currentViewController = vc2;
} ...
Problem
The View Controller in the first UITabBarItem always works as expected, extending it to the full size of thew view.
However, in the second and following tabs, this doesn't happen: the view doesn't extends. This shows if, for example, I align a tab with the bottom in the ViewController2 XIB: this will not be at the bottom when viewed inside the UITabBarItem.
Note
Please note that this is not related to the XIB: if I invert ViewController1 and ViewController2, it will be ViewController1 the one failing to extend. It's related to the UITabBarItem.
Ideas
Possibly, this depends by the way I addSubview when I call the DashBoardViewController's initWithNibName. But I can't find a way to explain this.
Other details
All the XIB are set with "Size = none".
I can't really speak to the way you have your XIB setup without seeing it, but I can make a couple of suggestions.
The behaviour that you're trying to implement by removing & adding subviews to DashBoardViewController should really be handled by a UITabBarController. This provides a UITabBar, a view for your content and handles the logic of switching between UIViewControllers while keeping layout sane and being part of the SDK.
If for some reason you can't, or don't want to use a UITabBarController, I'd suggest implementing a viewWillLayoutSubviews method on your DashBoardViewController, like so:
- (void)viewWillLayoutSubviews
{
if( self.currentViewController )
{
self.currentViewController.view.frame = self.view.bounds;
}
}
Maybe also try adding the self.currentViewController.view.frame = self.view.bounds; line after you've swapped ViewControllers too, for good measure. This will make sure that the frame of your current ViewController's view is always sized to fill the bounds of DashBoardViewController's view.
This isn't the 'Proper' way to do it though, I'd really recommend using a UITabBarController if you can, since you don't know how much else of UITabBarController you'll end up re-implementing if you start rolling your own controller.
Any further problems will most probably be to do with the internal layout of your sub-ViewControllers, rather than their size / position in DashBoardViewController's view.
On your XIB File make sure that your set the flexible height to stick to top and bottom, this way the UITableView will always have the same height as the 4" display

UITableView width when inside a popover

See below - the tableView cells are getting cut off. Why doesn't this work? The width of the popover is 240.
(In a subclass of UITableViewController)
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.frame = CGRectMake(0,0,200,200);
}
You have to specify the content size of the ui controller you are displaying. You can do it in 2 ways:
access the ui controller from your popover controller and set it size:
UIViewController* yourViewController = yourPopOverController.contentViewController;
yourViewController.contentSizeInViewController = CGSizeMake(300, 600);
check the checkbox "Use Explicit Size" for popover in the inspector of the viewController in storyboard
As you see the content ui controller is the one responsible of setting the size of your popover.
Have you tried specifying the popover's content size?
i.e.
self.popoverController.popoverContentSize = CGSizeMake(400, 500);
I've found that while popover's are "suppose" to adjust to the appropriate size based on the content view controller, this doesn't always work as well as it should.

Resources