Embedding a UIViewController with dynamic height inside parent view controller - ios

I am displaying a UIViewController(In NotificationContentExtension) with a child view controller. The parent controller is just adding to its view the view of child controller as subview.
The subview is all built with autolayout and constraints, which renders fine when not embedded in a view controller.
Currently, the parent notification is displayed as big as the parent view controller's view is configured to be in the extension's Info.plist file.
If I provide a height to the childcontroller's view in the parent view it renders as big as the parent but in case I don't initialise its frame, it doesn't render the child controller's view elements.
What I am seeking is to open the controller as big as the child view container's dynamic height make it to be.

To clarify, I believe what you're trying to achieve is a Modal "popup" View Controller than is smaller (in some way) than the View Controller that calls it, similar to the functionality of an Alert View Controller...
Go to your Storyboard and click on the child view controller with the custom height, then open the right pane to view the Size Inspector. I'm assuming here your Simulated Size = "Freeform" with your height and width. This will allow you to actually resize the View Controller in the Storyboard but the view controller will show up fullscreen.
Now click on the Attributes Inspector and in the 4th grouping down, check the box for Content Size "Use Preferred Explicit Size" and type in your simulated size. This will allow your Modal view controller to show up at the exact size you're asking.
To do this programmatically, try this in the calling ViewController's prepare for segue implementation:
destinationVC.preferredContentSize = CGSize(width: 300.0, height: 400.0)
Cheers :)

Related

"Freeform" size in IB - Storyboard

As I understand and tried, if I change the simulated size of the ViewController in the storyboard it will still stretch its main "view" property to the whole screen.
So my question is when and why should I change this "Freeform" property?
Whenever I needed to make some overlay, I just make the main "view" transparent and create another view on top of that with my content, then I present it I do it as a container.
As I see there is no need for it, And I couldn't find anywhere in Apple documentation about it.
This property is only for simulating a different size.
ViewControllers do not always have to be fullscreen containers, for example View Controller containment, or the new iOS 7 View Controller transitions may lead to a View Controller that's size covers some partial area of the screen.
You would use the 'freeform' mode to change the size in interface builder, so you can simulate what the end size would be, whilst you work with it.
This was answered in the Q UIViewController Freeform size issue: uncheck "Resize View From NIB" in your View Controller's Attributes Inspector and your freeform view controller's view will indeed be decoded at the size you designed in your storyboard.
You are not quite on the right track. If you use Freeform for the size of the view controller, you can specify the desired size of your controller.
Also it's really important how you use the controller, how you set the springs & struts or layout constraints. If you set the springs or constraints to stretch your controller's view to "match" the parent size then the view will be stretched, if not it will keep the size you mentioned but not in all cases. For example if you present the view controller modally and you specify a different explicit size in IB then the view will have this size.
Usually you use the freeform property when you add the view controller as a child to another view controller programatically and you really want to have that fixed size.
If you push the view controller or you present it as modal view controller (and you use the modal presentation styles) then there is no need to use freeform.
Also another use of the freeform property is to preview the actual size of your view controller when is presented as a modal view controller using existent presentation styles.
Another wrongly implementation that I seen before regarding the size of the view controller's view is to check the size in viewDidLoad. Always check the size in viewWillAppear|| viewDidAppear.

instantiateViewControllerWithIdentifier and freeform view

I'd like to create a sort of popup view that is displayed in a main controller through custom transitions.
I create the popup viewcontroller into a storyboard and I set its view as freeform, then I resize the view.
My problem is that when I load the view controller using instantiateViewControllerWithIdentifier the view bounds are always reset to full screen.
Here is the code that I'm using, the popUp size should be 100x100, but from this code I get 320X480.
self.popUp = (PopUpViewController*)[storyBoard instantiateViewControllerWithIdentifier:#"Popup"];
NSLog(#"%#",NSStringFromCGRect(self.popUp.view.bounds));
How can I setup a freeform view controller and load it programmatically with the right size?
Solution at Wrong frame value while accessing the view from storyboard in IOS worked for me.
Uncheck the "Resize View From Nib" box in the inspector. That should
give you the correct size for a freeform view controller.

Popover size with UINavigationController in Storyboard

I have created an iPad app containing a popover view embedded in a navigation controller. I like using Storyboard as much as possible, and setting the "Use Explicit Size" in Xcode give me the correct size of my popover on the iPad. But it does not resize the UINavigationController view and its embedded UIViewControllers when viewing the storyboard in Xcode.
This is quite annoying as the size of my view in Xcode does not correspond to the actual size of the popover.
So, my problem is not setting the correct size of the popover (i also know i can do that with code), but I want the views in storyboard to show the actual size of my views.
What is the correct way to create viewControllers embedded in a navigation controller using storyboard?
For the views embedded in the navigation controller, you can change their size in the storyboard by doing the following:
Select the View Controller.
Go to the Attributes Inspector under Simulated Metrics and select Size: Freeform.
Select the View Controller's view.
Go to the Size Inspector and change the width and height.
After that, the view should be sized correctly in the storyboard.
Unfortunately I do not know how to change the size of the navigation controller.

How to make the target view a subview of window using storyboard segue?

I am using storyboard and trying to segue from a bar view to a full screen view where the bar view's parent view is the same size as the bar view and is clipping subviews, but I'd like the destination view to take the full screen. Is there an easy way to do this by setting the properties of either the segue or the destination view controller? What is the proper way to do this? I really would like to make the full screen view a subview of window.
Thanks for any suggestions!
Luverio
I had to go around the segue by manually instantiate the target view controller and add it as the window's rootViewController.

How to add a segmented control anchored to the bottom of the screen below a table view in interface builder

I have a view controller (derived from UIViewController not UITableViewController) containing a table view. The controller and view are both within the same xib.
I want to add a segmented control to appear at the bottom of the screen, with the table view taking up the rest of the space above it.
However in IB it will not let me place the segmented control as a direct child of the view controller, it will only let me place it as a child of the table view i.e. the hierarchy looks like this:
Navigation Controller
Navigation Bar
Main View Controller - Item
Table View
Segmented Control - First, Second
Navigation Item - Item
Bar Button Item - Style
If I run it like this then the segmented control floats up and down depending upon how many items there are in the table view.
What can I do to get it to be locked to the bottom of the screen?
UPDATE:
I got nearer - I added a view as a child of the main view controller and made the table view and segment control children of that, but now only the top half of the control appears when I run the simulator but everything looks fine in IB, why is there a discrepency?
Side question - as a starting point I'm using one of Apple's sample project, why was there no main view? When should you and when should you not have a main view?
Thanks
Just open your xib and expand your view and drag Tableview control under View and also drag segmented control under view not in tablview this will work.
you need to set frame of both in you viewDidLoad
tblview.frame = CGRectMake(0, 0, 320, 400);
sgmntcontrol.frame = CGRectMake(50, 405,150 ,50)];

Resources