Displaying a modal on top of a fullscreen view controller in the background - swift

I want to achive a viewcontroller modal such as the Apple HandOff HomePod UI style.
It should be a modal on top of the current ViewController (which is and should stay in fullscreen) and should have a modal on top of itself.
The result should look like this, with the same corner radius of the device:
I already tried shrinking the modal view controller, using UIViewControllerTransitioningDelegate, with this code, but couldn't apply a corner radius to the ViewController or NavigationController:
extension ViewController: UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return ShrinkedPresentationController(presentedViewController: presented, presenting: presentingViewController)
}
}
class ShrinkedPresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
guard let bounds = containerView?.bounds else { return .zero }
return CGRect(x: 0, y: 60, width: bounds.width, height: bounds.height - 60)
}
}

Related

Working with 2 UINavigationControllers inside a ViewController as Child causes only 1 to respond, the other does not respond

I'm working with a project where I start with a ContainerViewController having 2 controllers added as a child:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let scene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: scene)
window?.rootViewController = ContainerViewController()
window?.makeKeyAndVisible()
}
The ContainerViewController:
class ContainerViewController: UIViewController {
var panGestureRecognizer: UIPanGestureRecognizer!
private var menuWidth: CGFloat = UIScreen.main.bounds.width - 50
let menuController = SideMenuViewController()
let mainController = MainViewController()
override func viewDidLoad() {
super.viewDidLoad()
addChildVCs()
panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
view.addGestureRecognizer(panGestureRecognizer)
}
private func addChildVCs() {
addChild(menuController)
menuController.view.frame = CGRect(x: 0 - view.frame.size.width, y: 0, width: menuWidth, height: view.frame.size.height)
view.addSubview(menuController.view)
menuController.delegate = self
menuController.didMove(toParent: self)
addChild(mainController)
mainController.delegate = self
view.addSubview(mainController.view)
mainController.didMove(toParent: self)
}
With this panGesture I just show/hide a SideMenu, nothing strange here.
Inside this MainViewController, I have a TabBarController named mainController and another UINavigationController that store user conversations, named ConversationViewController(), both added as a child like this:
class MainViewController: UIViewController {
let mainController = MainTabController()
let conversationNavigationController = UINavigationController(rootViewController: ConversationViewController())
override func viewDidLoad() {
super.viewDidLoad()
addChildVCs()
}
private func addChildVCs() {
addChild(mainController)
mainController.menuDelegate = self
view.addSubview(mainController.view)
mainController.didMove(toParent: self)
addChild(conversationNavigationController)
conversationNavigationController.view.frame = CGRect(x: view.frame.size.width, y: 0, width: view.frame.size.width, height: view.frame.size.height)
view.addSubview(conversationNavigationController.view)
conversationNavigationController.didMove(toParent: self)
}
The conversationNavigationController origin.x point is starting at the end of the mainController (tabBar) so I can panGesture to the right and show/hide this other UINavigationController.
The problem is when I start the execution I can interact with the mainController(tabBarController) but when I swipe to the right and I have the conversationNavigationController on screen, it doesn't respond to any actions.
Basically I'm working with 2 navigationControllers at the same time but I don't understand why 1 is working and the other one does not respond to any actions.
Any clue on what's the problem? I can provide more code if needed!
Thanks in advance
If you just add your child view controller's content views to your MainViewController's content view, Auto Layout doesn't know what to do with them. At the drop of a hat, Auto Layout will resize your child view controllers in unexpected and undesired ways.
I'm guessing that your view controllers' view frames are getting messed up. It might be that there is some other problem with your view controllers that's preventing them from responding to events, but first I would rule out view layout problems.
I would suggest adding container views to your main view controller, including Auto Layout constraints to put them where you want them.
Then add your child view controllers' views as subviews of those container views, and anchor all 4 edges of your child view controller's view to the edges of their parent views.
When you're first working on it, it is worth adding a borderWidth and borderColor to your different views' layers so you can see what's going on.

How to present a ViewController as a Modal Sheet without the background shadow

Is there any way to present a ViewController as a Modal Sheet without the background shadow as shown in the first image below using swift. Is there an easy way or should we need to write custom UIPresentationController? [![The required output][1]][1]
[1]: https://i.stack.imgur.com/QAEEn.png![enter image description here](https://i.stack.imgur.com/q4JD5.jpg)
You can use a smaller size view controller as per your need. Firstly add a class.
class SmallSizePresentationController : UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
get {
guard let theView = containerView else {
return CGRect.zero
}
return CGRect(x: 0, y: theView.bounds.height * (281 / 896), width: theView.bounds.width, height: theView.bounds.height)
}
}
}
Then when you want to present this type of view controller just add the extension of your current view controller.
extension YourCurrentViewController: UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return SmallSizePresentationController(presentedViewController: presented, presenting: presenting)
}
}
Then present your new view controller like this
let VC = withoutShadowVC()
VC.transitioningDelegate = self
VC.modalPresentationStyle = .custom
self.present(VC, animated: true, completion: nil)
You can modify the view controller height.

How to constrain second NSViewController minimum size in OS X app?

I am a novice Mac OS X developer. I assume this is an easy question, but I haven't been able to find any useful results via searches.
How do I constrain the size of a SECOND view controller?
I started with a simple Mac OS X app, with a single View Controller. I can select the window that contains the View Controller, then select the "Size Inspector" and check the "Minimum Content Size" box, and specify a minimum x and y for the window.
This allows me to specify the minimum size for this first view controller, as I expect. All is good.
Then I add a second view controller, with a Modal segue from the first view controller, triggered by a button press. I add a NSTextView to this second view controller, to display an attributed string. The text view works fine, displaying the attributed string correctly. This text view is a separate window, and has no minimum size constraint.
So how do i specify the minimum size for this second view controller view? Is this typically done in Interface Builder, or programmatically? When I step through the view hierarchy using Document Outline, I don't see how I can specify minimum size using the Size Inspector. Am I missing something??
Here is my simplified code:
file "ViewController.swift"
class ViewController: NSViewController {
...
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
let secondVC: SecondViewController = segue.destinationController as! SecondViewController
secondVC.reportAttrString = myReport.reportText
}
...
}
file "SecondViewController.swift"
class SecondViewController: NSViewController {
var reportAttrString = NSMutableAttributedString()
#IBOutlet var ReportTextView: NSTextView!
}
I would appreciate any suggestions, or pointers to any documentation or tutorials that may help me.
The simplest way to do this would be:
class SecondViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
self.view.window?.delegate = self
self.view.window?.minSize = NSSize(width: 100, height: 100)
}
}
override func viewDidAppear() {
super.viewDidAppear()
var frame = self.view.window!.frame
var initialSize = NSSize(width: 100, height: 100)
frame.size = initialSize
self.view.window?.setFrame(frame, display: true)
}
Although if you were looking for a manual approach then the following would work aswell.
class SecondViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
self.view.window?.delegate = self
// Set the initial size
var frame = self.view.window?.frame
var initialSize = NSSize(width: 100, height: 100)
frame.size = initialSize
self.view.window?.setFrame(frame, display: true)
}
func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
let minimumSize = NSSize(width: 100, height: 100)
var newSize = NSSize()
if(frameSize.width < minimumSize.width) {
newSize.width = minimumSize.width
} else {
newSize.width = frameSize.width
}
if(frameSize.height < minimumSize.height) {
newSize.height = minimumSize.height
} else {
newSize.height = frameSize.height
}
return newSize
}
}
Further reading:
Resize windows event
NSWindow minSize
Resizing the window

How to create view controller that covers half the screen programmatically SWIFT

I am trying to create a view similar to Facebook so that when you click a button, a view controller covers half the sceeen like this:
And then if you swipe up it covers the whole view like this:
How would I go about doing this?
you should use an container view , and set frame of container view half of the screen height
. but you can just use container view in object library xcode.
container view is look like view use in your view controller class bellow the class name add this code :
class YourViewController: UIViewController {
// MARK: Properties
let containerView = UIView()
in your viewDidLayoutSubviews() function you should set frame of container view like this :
override func viewWillLayoutSubviews() {
containerView.frame = CGRect(x: 0, y: self.view.frame.midY, width: self.view.frame.width, height: self.view.frame.height / 2)
let yourSecondViewController = YourSecondViewController()
addContentContainerView(yourSecondViewController)
}
now you have a container view that cover half of the screen,
then you should add your second view controller to your container view ,
so you should create a second view controller class programmatically , or you should create a view controller in your xcode storyboard and set storyboard id for that.
for add and remove a child view controller in an container view you can use this functions:
private func addContentContainerView(_ childViewController: UIViewController) {
childViewController.willMove(toParentViewController: self)
containerView.addSubview(childViewController.view)
self.addChildViewController(childViewController)
childViewController.didMove(toParentViewController: self)
}
private func removeContentContainerView(_ childViewController: UIViewController) {
childViewController.didMove(toParentViewController: nil)
childViewController.view.removeFromSuperview()
childViewController.removeFromParentViewController()
}
then you should add your second view controller to your container view with private func addContentContainerView(_ childViewController: UIViewController)
if you make the second programmatically and set you should use this code for use add that in your container in your viewWillLayoutSubviews method like this:
override func viewWillLayoutSubviews() {
containerView.frame = CGRect(x: 0, y: self.view.frame.midY, width: self.view.frame.width, height: self.view.frame.height / 2)
let yourSecondViewController = YourSecondViewController()
addContentContainerView(yourSecondViewController)
}
but if you make your second view controller in storyboard you should set id for that , select view controller then select identity inspector below identity set Storyboard ID : SecondViewController
then instead last viewWillLayoutSubwies , your viewWillLayoutSubviews should like this:
override func viewWillLayoutSubviews() {
containerView.frame = CGRect(x: 0, y: self.view.frame.midY, width: self.view.frame.width, height: self.view.frame.height / 2)
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let yourSecondViewController = mainStoryBoard.instantiateViewController(withIdentifier: "SecondViewController")
addContentContainerView(yourSecondViewController)
}
and for scroll that you should add a UIScrollView and set height of that to self.view.frame.height * 1.5

How to modify slide transition

I had asked a question similar to this once before but I didn't realize and don't think I can use the same method due to the nuances of the two methods. (How to stop slide transition into Second VC before it covers UIView?)
I ultimately learned how to do the transition using APPCoda's lesson(http://www.appcoda.com/custom-segue-animations/)
The original question resulted in an answer providing a solution using container views and hard coded views. What I am wondering is if I can get the same effect using two separate view controllers and linking them through a segue with a gesture recognizer.
What I would like to accomplish is:
Have my initial view controller
Tap Button and have Second View Controller Overlap the first View Controller Partially (By partially I mean I have a UIView on the first View Controller that I want to remain visible. So the top of the second view controller will slide up until it hits the bottom of the UIView).
What I currently have is the original view controller being pushed up and out of the screen by the second view controller sliding up from the bottom of the screen.
Code that handles the transition using a segue from one VC to the sliding VC:
import Foundation
class CustomSegueToSecondVC: UIStoryboardSegue
{
override func perform() {
let originalVC = self.sourceViewController .view as UIView!
let slidingVC = self.destinationViewController.view as UIView!
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
slidingVC.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)
let window = UIApplication.sharedApplication().keyWindow
window?.insertSubview(slidingVC, aboveSubview: originalVC)
UIView.animateWithDuration(0.4, animations: { () -> Void in
originalVC.frame = CGRectOffset(originalVC.frame, 0.0, -screenHeight)
slidingVC.frame = CGRectOffset(slidingVC.frame, 0.0, -screenHeight)
}) { (Finished) -> Void in
self.sourceViewController.presentViewController(self.destinationViewController as UIViewController, animated: false, completion: nil)
}
}
};