I'm developing an app for iOS.
I want to have a view controller (called child) that appears inside another view in my main view controller (called parent).
I managed to do this by adding a container view which embeds the child view controller successfully.
In my child view controller I have a button that opens another (3rd) view controller.
I want this screen to open within the bounds of the view container - but it opens on a full screen.
Is there a way I can define the view container in a way that all segues inside of it will show inside its bounds?
definesPresentationContext - a UIViewController's property that might be helpful here.
A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.
https://developer.apple.com/reference/uikit/uiviewcontroller/1621456-definespresentationcontext
Set the child's property to true:
childViewController.definesPresentationContext = true
Also, before presenting the viewController (3rd), set its modal presentation style:
viewControllerToBePresented.modalPresentationStyle = .overCurrentContext
Hope this helps!
Try this post. It might help for adding viewcontrollers within bounds.
http://khanlou.com/2015/04/view-controllers-in-cells/
Related
I was going through Apple's documentation about navigation controller and find this point ambiguous and hard to comprehend.
It was written in this online documentation of navigation controller.
Navigation Controller Views
A navigation controller is a container view controller—that is, it
embeds the content of other view controllers inside of itself. You
access a navigation controller’s view from its view property. This
view incorporates the navigation bar, an optional toolbar, and the
content view corresponding to the topmost view controller. Figure 2
shows how these views are assembled to present the overall navigation
interface. (In this figure, the navigation interface is further
embedded inside a tab bar interface.) Although the content of the
navigation bar and toolbar views changes, the views themselves do not.
The only view that actually changes is the custom content view
provided by the topmost view controller on the navigation stack.
From that, my understanding is that inside this "view" property. There should be at least two subview inside this view.One is the navigationBar the other is the contentView of the current displayed viewController’s view. But while I am debugging only the navigation bar showed with another view called UINavigationTransitionView showed.
My question is, is this normal. Have I done anything wrong?
Second, what is the most common way to access current displayed viewController's view with only the reference to the navigation controller.
Thanks
UINavigationTransitionView controller contains one wrapper view which intern will have the current uiviewcontroller's view.
You can probably find this view as a subview of UINavigationTransitionView. However this is not the "right" way to do this. The proper way is to go through property "topViewController" and then take its view:
self.navigationController.topViewController.view
If there is another view controller or its view that you need, you have access to whole view controller's hierarchy across navigation controller through viewControllers property.
self.navigationController.viewControllers
More here:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationController_Class/index.html
I have 3 View Contorller
First View controller have Container View of height 300.0f at the center.
It has one embedded view Controller which is table View controller.
On cell selection it should navigate to detailsViewController.
All the process is ok.
But detailsViewController is not behaving as embedded view controller of containerView and not of same size as container view.
It takes whole screen size.
As it is triggered from the embedded View Controller it should follow that frame without overlapping other controls which are in First View Controller.
You need to embed not view controller with table view, but embed navigationController(you can hide navigation panel on up side), end set yours table view controller as root for it, and use pushViewController to go to detail page.
hope ganesh this will help you
Look You have a Container View inside it you have a tableView and By clicking an cell you pushing another viewController that what i concluded after reading your question(hope you done everything in modular way).
Now you must have UIView subclass(i.e separately creating a view class ) inside which you have a tableView so you pushing by
[self.nav pushViewController:sos animated:YES];
there are two way to push ViewController inside a view first by Callback(through blocks) or passing Navigation ref. in UIView Class .So you are pushing the new Controller over the navigation thats why it showing this behavior and which is obvious.
ViewController has definesPresentationContext and providesPresentationContextTransitionStyle,
but have no idea about how they work.
I watched the WWDC2011, but I'm still confused about these two things.
Could anyone explain this, preferably with a simple demo?
Both properties are used for view controller containment, and provide child view controllers the option to override the presentation context or presentation style of the window's root view controller. For reference, here is the relevant WWDC presentation that provides an explanation of both:
https://developer.apple.com/videos/play/wwdc2011-102/
definesPresentationContext is used to decide which view controller will determine the size of the presented view controller's view:
When a view controller is presented, iOS starts with the presenting
view controller and asks it if it wants to provide the presentation
context. If the presenting view controller does not provide a context,
then iOS asks the presenting view controller's parent view controller.
iOS searches up through the view controller hierarchy until a view
controller provides a presentation context. If no view controller
offers to provide a context, the window's root view controller
provides the presentation context.
If a view controller returns YES, then it provides a presentation
context. The portion of the window covered by the view controller's
view determines the size of the presented view controller's view. The
default value for this property is NO.
providesPresentationContextTransitionStyle is used to decide which modal presentation style should be used when presenting a child view controller:
When a view controller’s definesPresentationContext property is YES, it can replace the transition style of the presented view controller with its own. When the value of this property to YES, the current view controller’s transition style is used instead of the style associated with the presented view controller. When the value of this property is NO, UIKit uses the transition style of the presented view controller. The default value of this property is NO.
For complex child view controllers such as UISearchController, it's a good idea to have these set to true, the default value is false.
definesPresentationContext is related to child view controllers which in storyboards are represented by Container View. So, this option influence a presentation of a new UIViewController from a container view.
providesPresentationContextTransitionStyle is related also to child view controllers and will take effect only when definesPresentationContext is true
Can I reveal the underlying view (or the view at the back of a view controller), by making a view controller's view transparent? I tried and it just fades to black and doesn't reveal anything behind it.
Reason?
EDIT:
Okay, this question needs more context.
I have a view controller. Now I am going to present another view controller(simple presentation of view controller, modally). After the new view controller has been presented, I am making its view transparent with alpha=0. Why does it not reveal the underlying view controller's view?
Will using the iOS 7 Transition API help?
If you present your view controller modally (using presentViewController:animated:completion:) then the presenting view controller's view will be removed removed from the window, therefore you cannot see through your presented view controller to the presenting view controller. You might want to use child view controller: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
Alternatively, you can present your view controller inside a new UIWindow.
You cannot see the original view controller because you use -presentViewController:animated:completion: which will hide the presenting view controller after the animation finished.
You can set the modalPresentationStyle to one of UIModalPresentationOverCurrentContext, UIModalPresentationOverFullScreen and UIModalPresentationCustom before you called 'presentViewController:animated:completion' so that the underlying presenting view controller will not be hidden.
If you would like to have the same background image showing below all UIViewController you can add a UIImageView to the [UIApplication sharedApplication].window in the applicationdidfinishlaunching method
In my app I want to add a view much like the status bar, always onscreen at the top of each of my view controllers, displaying application wide data.
I really have no clue how I might achieve this so any suggestions would be really helpful. I;m sure someone must have chased this idea at some point?
Thanks.
What you're willing to do is fairly straightforward since iOS 5 using view controller containment, it allows you to embed child view controllers in a parent view controller.
In your case I would create a custom container view controller with two subviews: the content view and the statusbar-like view. The content view should display your current root view controller by adding it as a child view controller to your container view controller and adding its view as a subview to the content view. The statusbar-like view can then be used to display information that will be visible everywhere in your app.
You might want to read this documentation for further details:
Creating Custom Container View Controllers
You can create your custom view and add it as a subview to your keyWindow. This way it will always be visible. Another option is if you have tab bar controller or a navigation controller as your root view controller, then you can add that view as a subview to their respective view and it will always be visible.