Skip to content

Commit

Permalink
Working example
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Gilmore committed Aug 21, 2019
1 parent f139b70 commit 3a3b12e
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 40 deletions.
12 changes: 0 additions & 12 deletions NGPopoverForceResizeTest.xcodeproj/project.pbxproj
Expand Up @@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */
D0C9F30F2306046B00539842 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C9F30E2306046B00539842 /* AppDelegate.swift */; };
D0C9F3112306046B00539842 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C9F3102306046B00539842 /* ViewController.swift */; };
D0C9F3142306046B00539842 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D0C9F3122306046B00539842 /* Main.storyboard */; };
D0C9F3162306046E00539842 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D0C9F3152306046E00539842 /* Assets.xcassets */; };
D0C9F3192306046E00539842 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D0C9F3172306046E00539842 /* LaunchScreen.storyboard */; };
/* End PBXBuildFile section */
Expand All @@ -18,7 +17,6 @@
D0C9F30B2306046B00539842 /* NGPopoverForceResizeTest.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = NGPopoverForceResizeTest.app; sourceTree = BUILT_PRODUCTS_DIR; };
D0C9F30E2306046B00539842 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D0C9F3102306046B00539842 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
D0C9F3132306046B00539842 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
D0C9F3152306046E00539842 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
D0C9F3182306046E00539842 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
D0C9F31A2306046E00539842 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -56,7 +54,6 @@
children = (
D0C9F30E2306046B00539842 /* AppDelegate.swift */,
D0C9F3102306046B00539842 /* ViewController.swift */,
D0C9F3122306046B00539842 /* Main.storyboard */,
D0C9F3152306046E00539842 /* Assets.xcassets */,
D0C9F3172306046E00539842 /* LaunchScreen.storyboard */,
D0C9F31A2306046E00539842 /* Info.plist */,
Expand Down Expand Up @@ -124,7 +121,6 @@
files = (
D0C9F3192306046E00539842 /* LaunchScreen.storyboard in Resources */,
D0C9F3162306046E00539842 /* Assets.xcassets in Resources */,
D0C9F3142306046B00539842 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -143,14 +139,6 @@
/* End PBXSourcesBuildPhase section */

/* Begin PBXVariantGroup section */
D0C9F3122306046B00539842 /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
D0C9F3132306046B00539842 /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
D0C9F3172306046E00539842 /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
Expand Down
3 changes: 3 additions & 0 deletions NGPopoverForceResizeTest/AppDelegate.swift
Expand Up @@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
return true
}

Expand Down
24 changes: 0 additions & 24 deletions NGPopoverForceResizeTest/Base.lproj/Main.storyboard

This file was deleted.

2 changes: 0 additions & 2 deletions NGPopoverForceResizeTest/Info.plist
Expand Up @@ -22,8 +22,6 @@
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
Expand Down
149 changes: 147 additions & 2 deletions NGPopoverForceResizeTest/ViewController.swift
Expand Up @@ -8,13 +8,158 @@

import UIKit

class ViewController: UIViewController {
class LargeViewControllerWithNavBar: UIViewController {
var button: UIButton! = nil
var heightConstraint: NSLayoutConstraint! = nil

override func viewDidLoad() {
self.view.backgroundColor = .green
self.title = "Title Here"

button = UIButton()
button.setTitle("Tap to expand", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(didTap), for: .touchUpInside)
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false

button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true

self.view.widthAnchor.constraint(equalToConstant: 600).isActive = true
heightConstraint = self.view.heightAnchor.constraint(equalToConstant: 400)
heightConstraint.isActive = true
}

func setPreferredContentSizeFromAutolayout() {
let size = self.view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
let contentSize = CGSize(
width: max(
size.width,
100
),
height: size.height
)
self.preferredContentSize = contentSize
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.navigationController?.setNavigationBarHidden(false, animated: true)
self.setPreferredContentSizeFromAutolayout()
}

@objc private func didTap() {
heightConstraint.constant = 500
setPreferredContentSizeFromAutolayout()
}
}


class SmallViewControllerNoNavBar: UIViewController {
var button: UIButton! = nil
override func viewDidLoad() {
self.view.backgroundColor = .red

button = UIButton()
button.setTitle("This view is 300x300", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(didTap), for: .touchUpInside)
self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false

button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true

self.view.widthAnchor.constraint(equalToConstant: 300).isActive = true
self.view.heightAnchor.constraint(equalToConstant: 300).isActive = true
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

self.navigationController?.setNavigationBarHidden(true, animated: false)

let size = self.view.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
let contentSize = CGSize(
width: max(
size.width,
100
),
height: size.height
)
self.preferredContentSize = contentSize
}

@objc private func didTap() {
self.navigationController?.pushViewController(LargeViewControllerWithNavBar(), animated: true)
}
}

final class PopoverPushNavigationController: UIViewController {
private let wrappedNavigationController: UINavigationController

init(rootViewController: UIViewController) {
self.wrappedNavigationController = UINavigationController(rootViewController: rootViewController)
super.init(nibName: nil, bundle: nil)
self.wrappedNavigationController.delegate = self
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
wrappedNavigationController.willMove(toParent: self)
self.addChild(wrappedNavigationController)
self.view.addSubview(wrappedNavigationController.view)
}

override func preferredContentSizeDidChange(forChildContentContainer container: UIContentContainer) {
self.preferredContentSize = container.preferredContentSize
}
}

extension PopoverPushNavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
// Set our preferred content size so that UIKit knows to animate down to the new view controller's
// preferred content size
navigationController.preferredContentSize = viewController.preferredContentSize

// If there are no toolbar items, we don't want a nav bar
navigationController.setToolbarHidden(viewController.toolbarItems?.isEmpty ?? true, animated: animated)
}
}

class ViewController: UIViewController {
var button: UIButton! = nil

override func viewDidLoad() {
super.viewDidLoad()

button = UIButton()
button.setTitle("Show popover", for: .normal)
button.setTitleColor(.blue, for: .normal)

self.view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
button.addTarget(self, action: #selector(didTap), for: .touchUpInside)

self.view.backgroundColor = .white
}

@objc private func didTap() {
let firstVC = SmallViewControllerNoNavBar()
let containerController = PopoverPushNavigationController(rootViewController: firstVC)
containerController.modalPresentationStyle = .popover
containerController.popoverPresentationController?.sourceRect = button.frame
containerController.popoverPresentationController?.sourceView = self.view
containerController.popoverPresentationController?.permittedArrowDirections = .up
self.present(containerController, animated: true)
}
}

0 comments on commit 3a3b12e

Please sign in to comment.