Add internal ContainerView class
ADD
swift/Samples/SplitScreenContainer/Sources/ContainerView.swift
+/*
+* Copyright 2019 Square Inc.
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+import UIKit
+
+internal class ContainerView: UIView {
+
+ var contentView: UIView = .init()
+
+ override init(frame: CGRect) {
+ super.init(frame: frame)
+
+ commonInit()
+ }
+
+ required init?(coder: NSCoder) {
+ super.init(coder: coder)
+
+ commonInit()
+ }
+
+ private func commonInit() {
+ self.addSubview(contentView)
+ }
+
+ override func layoutSubviews() {
+ super.layoutSubviews()
+
+ contentView.frame = self.bounds
+
+ contentView.subviews.forEach({ $0.frame = self.bounds })
+ }
+}

MODIFY
swift/Samples/SplitScreenContainer/Sources/SplitScreenContainerViewController.swift
swift/Samples/SplitScreenContainer/Sources/SplitScreenContainerViewController.swift
@@ -20,12 +20,12 @@ public final class SplitScreenContainerViewController<LeadingScreenType: Screen,
public typealias ContainerScreen = SplitScreenContainerScreen<LeadingScreenType, TrailingScreenType>
private var leadingContentViewController: ScreenViewController<LeadingScreenType>? = nil
- private lazy var leadingContainerView: UIView = .init()
+ private lazy var leadingContainerView: ContainerView = .init()
private lazy var separatorView: UIView = .init()
private var trailingContentViewController: ScreenViewController<TrailingScreenType>? = nil
- private lazy var trailingContainerView: UIView = .init()
+ private lazy var trailingContainerView: ContainerView = .init()
private var currentRatio: CGFloat {
didSet {
@@ -91,6 +91,7 @@ public final class SplitScreenContainerViewController<LeadingScreenType: Screen,
self.leadingContentViewController = embed(screen.leadingScreen, in: leadingContainerView)
self.trailingContentViewController = embed(screen.trailingScreen, in: trailingContainerView)
+
update(with: screen)
}
@@ -103,30 +104,30 @@ public final class SplitScreenContainerViewController<LeadingScreenType: Screen,
let (leadingRect, separatorRect) = firstSlice.divided(atDistance: distance - currentSeparatorWidth, from: .minXEdge)
- let isRightToLeft = isLayoutDirectionRightToLeft()
-
- leadingContainerView.frame = isRightToLeft ? trailingRect: leadingRect
- leadingContentViewController?.view.frame = leadingContainerView.bounds
+ leadingContainerView.frame = isLayoutDirectionRightToLeft ? trailingRect: leadingRect
separatorView.frame = separatorRect
- trailingContainerView.frame = isRightToLeft ? leadingRect : trailingRect
- trailingContentViewController?.view.frame = trailingContainerView.bounds
+ trailingContainerView.frame = isLayoutDirectionRightToLeft ? leadingRect : trailingRect
+ }
}
- private func isLayoutDirectionRightToLeft() -> Bool {
+fileprivate extension UIViewController {
+ var isLayoutDirectionRightToLeft: Bool {
if #available(iOS 10.0, *) {
return traitCollection.layoutDirection == .rightToLeft
} else {
return UIView.userInterfaceLayoutDirection(for: view.semanticContentAttribute) == .rightToLeft
}
}
+}
- private func embed<ScreenType: Screen>(_ screen: ScreenType, in containerView: UIView) -> ScreenViewController<ScreenType> {
+fileprivate extension ScreenViewController {
+ func embed<ScreenType: Screen>(_ screen: ScreenType, in containerView: ContainerView) -> ScreenViewController<ScreenType> {
let viewController = viewRegistry.provideView(for: screen)
addChild(viewController)
- containerView.addSubview(viewController.view)
+ containerView.contentView.addSubview(viewController.view)
viewController.didMove(toParent: self)
return viewController

Add internal ContainerView class