Skip to content

Preliminary SwiftUI support #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ContainerControllerSwift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'ContainerControllerSwift'
s.version = '1.1.2'
s.version = '1.1.4'
s.summary = 'This is a swipe-panel from application: https://www.apple.com/ios/maps/'

# This description is used to generate tags and improve search results.
Expand All @@ -33,7 +33,7 @@ TODO: Add long description of the pod here.
# s.ios.deployment_target = '13.0'
s.platform = :ios, "13.0"

s.source_files = 'ContainerControllerSwift/**/*.{swift}'
s.source_files = 'Sources/**/*.{swift}'

s.framework = "UIKit"
# s.ios.framework = 'UIKit'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ class MapsViewController: StoryboardController, MapsContainerControllerDelegate,

@objc func rotated() {

let orint = UIDevice.current.orientation
if orint == .faceUp || orint == .faceDown || orint == .portraitUpsideDown { return }
if !UIDevice.current.orientation.isRotateAllowed { return }

updateMapViewTopPadding()
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func containerControllerMove(_ containerController: ContainerController, positio

## Author

<[email protected]> 📩| [mrustaa](https://github.com/mrustaa/)
<[email protected]> 📩| [mrustaa](https://github.com/mrustaa/) JUNE 2020

## License

Expand Down
35 changes: 33 additions & 2 deletions Sources/ContainerController/ContainerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// Copyright © 2020 mrustaa. All rights reserved.
//

#if arch(x86_64) || arch(arm64)

import UIKit
import SwiftUI

@available(iOS 13.0, *)
open class ContainerController: NSObject {
Expand All @@ -25,6 +28,8 @@ open class ContainerController: NSObject {

public var footerView: UIView?

public var hostingController: UIHostingController<AnyView>?

// MARK: Layout

public var layout: ContainerLayout = ContainerLayout()
Expand Down Expand Up @@ -211,8 +216,7 @@ open class ContainerController: NSObject {

@objc func rotated() {

let orint = UIDevice.current.orientation
if orint == .faceUp || orint == .faceDown || orint == .portraitUpsideDown { return }
if !UIDevice.current.orientation.isRotateAllowed { return }

if ContainerDevice.orientation == oldOrientation { return }
oldOrientation = ContainerDevice.orientation
Expand Down Expand Up @@ -452,6 +456,30 @@ open class ContainerController: NSObject {
calculationViews()
}

// MARK: - Add SwiftUI View

public func removeSwiftUIView() {
self.hostingController?.willMove(toParent: nil)
self.hostingController?.view.removeFromSuperview()
self.hostingController?.removeFromParent()
self.hostingController = nil
}

public func add<V: View>(swiftUIView: V, parentViewController: UIViewController? = nil) {
guard let contentView = self.view.contentView else {
return
}
removeSwiftUIView()
let hostingController = UIHostingController(rootView: AnyView(swiftUIView))
self.hostingController = hostingController
let parent = parentViewController ?? self.controller
parent?.addChild(hostingController)
hostingController.view.frame = contentView.bounds
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
contentView.addSubview(hostingController.view)
hostingController.didMove(toParent: parent)
}

// MARK: - Pan Gesture

@objc private func handlePan(_ gesture: UIPanGestureRecognizer) {
Expand Down Expand Up @@ -1351,3 +1379,6 @@ extension ContainerController: UIScrollViewDelegate {
}

}

#endif

4 changes: 4 additions & 0 deletions Sources/ContainerController/ContainerControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// Copyright © 2020 mrustaa. All rights reserved.
//

#if arch(x86_64) || arch(arm64)

import UIKit

@available(iOS 13.0, *)
Expand Down Expand Up @@ -36,3 +38,5 @@ public extension ContainerControllerDelegate {
}
}

#endif

15 changes: 13 additions & 2 deletions Sources/ContainerController/ContainerDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import UIKit

@available(iOS 13.0, *)
public extension ContainerDevice {

enum Orientation {
case portrait
case landscapeLeft
Expand Down Expand Up @@ -85,7 +84,7 @@ open class ContainerDevice {
switch UIDevice.current.orientation {
case .landscapeLeft, .landscapeRight:
portrait = false
case .portrait, .portraitUpsideDown:
case .portrait:
portrait = true
default: break
}
Expand Down Expand Up @@ -120,6 +119,18 @@ open class ContainerDevice {
return .landscapeLeft
}
}
}

public extension UIDeviceOrientation {

var isRotateAllowed: Bool {
return !(face || self == .portraitUpsideDown)
}

var face: Bool {
switch self {
case .faceUp, .faceDown: return true
default: return false
}
}
}