Skip to content

Commit

Permalink
fix safe area in sample code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazuhiro Hayashi committed Nov 19, 2017
1 parent 6aa3b2a commit 6df1f47
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 24 deletions.
6 changes: 5 additions & 1 deletion PagingKitTests/PagingMenuViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class PagingMenuViewControllerTests: XCTestCase {
let menuViewController = PagingMenuViewControllerTests.makeViewController(with: dataSource)
dataSource.registerNib(to: menuViewController)

menuViewController.loadViewIfNeeded()
menuViewController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 44)
menuViewController.reloadData(with: 0) { _ in
let resizedLength: CGFloat = 20
Expand All @@ -143,6 +142,11 @@ class PagingMenuViewControllerTests: XCTestCase {
wait(for: [expection], timeout: 1.0)
}

func testLoadView() {
let menuViewController = PagingMenuViewController(nibName: nil, bundle: nil)
menuViewController.loadViewIfNeeded()
}

static func makeViewController(with dataSource: PagingMenuViewControllerDataSource) -> PagingMenuViewController {
let menuViewController = PagingMenuViewController(nibName: nil, bundle: nil)
menuViewController.dataSource = dataSource
Expand Down
2 changes: 1 addition & 1 deletion iOS Sample/iOS Sample/ArbitraryNumberViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ArbitraryNumberViewController: UIViewController {
var contentViewController: PagingContentViewController!
var menuViewController: PagingMenuViewController!

var dataSource = [(menu: String, content: UIViewController)]()
var dataSource = [(menu: String, content: ContentTableViewController)]()

var startPosition = 0
var isSegmentedMenu = false
Expand Down
7 changes: 6 additions & 1 deletion iOS Sample/iOS Sample/ContentTableViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ import UIKit
class ContentTableViewCell: UITableViewCell {
@IBOutlet weak var emojiLabel: UILabel!
@IBOutlet weak var nameLabel: UILabel!

var additionalSafeAreaInsets: UIEdgeInsets = .zero

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}

override func layoutSubviews() {
super.layoutSubviews()
}

override func setSelected(_ selected: Bool, animated: Bool) {
Expand Down
12 changes: 8 additions & 4 deletions iOS Sample/iOS Sample/ContentTableViewController.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="fbn-3f-Ane">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13529" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="fbn-3f-Ane">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13174"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13527"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -25,7 +25,7 @@
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="reuseIdentifier" id="3Em-GE-Xap" customClass="ContentTableViewCell" customModule="iOS_Sample" customModuleProvider="target">
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="3Em-GE-Xap" id="222-Vs-rDE">
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="3Em-GE-Xap" id="222-Vs-rDE">
<rect key="frame" x="0.0" y="0.0" width="375" height="43.5"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
Expand Down Expand Up @@ -64,13 +64,17 @@
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="z6u-cs-lEJ" firstAttribute="leading" secondItem="Vff-7w-Wn7" secondAttribute="leading" id="7wg-ED-6Yk"/>
<constraint firstItem="z6u-cs-lEJ" firstAttribute="top" secondItem="Vff-7w-Wn7" secondAttribute="top" id="T00-Ho-niF"/>
<constraint firstAttribute="trailing" secondItem="z6u-cs-lEJ" secondAttribute="trailing" id="dhJ-1k-WOn"/>
<constraint firstItem="z6u-cs-lEJ" firstAttribute="leading" secondItem="Vff-7w-Wn7" secondAttribute="leading" id="f82-P5-fVP"/>
<constraint firstAttribute="bottom" secondItem="z6u-cs-lEJ" secondAttribute="bottom" id="xOJ-9c-G8b"/>
</constraints>
<viewLayoutGuide key="safeArea" id="NVr-cB-pAu"/>
</view>
<connections>
<outlet property="leadingLayouConstraint" destination="7wg-ED-6Yk" id="N9g-24-Yd9"/>
<outlet property="tableView" destination="z6u-cs-lEJ" id="2g4-SZ-1jg"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="5Oh-J2-0eB" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
Expand Down
29 changes: 20 additions & 9 deletions iOS Sample/iOS Sample/ContentTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
import UIKit

class ContentTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var leadingLayouConstraint: NSLayoutConstraint!

var leadingInset: CGFloat = 0 {
didSet {
leadingLayouConstraint?.constant = leadingInset
view.setNeedsLayout()
view.layoutIfNeeded()
}
}

var data: [(emoji: String, name: String)] = [
(emoji: "🐶", name: "Dog"),
(emoji: "🐱", name: "Cat"),
Expand All @@ -52,12 +62,16 @@ class ContentTableViewController: UIViewController, UITableViewDelegate, UITable

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem

if #available(iOS 11.0, *) {
tableView.insetsContentViewsToSafeArea = false
tableView.insetsLayoutMarginsFromSafeArea = false
}
}

override func updateViewConstraints() {
super.updateViewConstraints()
leadingLayouConstraint.constant = leadingInset
}

override func didReceiveMemoryWarning() {
Expand All @@ -66,7 +80,6 @@ class ContentTableViewController: UIViewController, UITableViewDelegate, UITable
}

// MARK: - Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
Expand All @@ -80,6 +93,4 @@ class ContentTableViewController: UIViewController, UITableViewDelegate, UITable
cell.configure(data: data[indexPath.row])
return cell
}


}
9 changes: 8 additions & 1 deletion iOS Sample/iOS Sample/DynamicSizeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DynamicSizeViewController: UIViewController {
var contentViewController: PagingContentViewController?


let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let dataSource: [(menu: String, content: ContentTableViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
return (menu: title, content: vc)
Expand All @@ -46,6 +46,13 @@ class DynamicSizeViewController: UIViewController {
contentViewController?.reloadData()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Expand Down
9 changes: 8 additions & 1 deletion iOS Sample/iOS Sample/FullscreenViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FullscreenViewController: UIViewController {
var contentViewController: PagingContentViewController!
var menuViewController: PagingMenuViewController!

let dataSource: [(menu: (title: String, color: UIColor), content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let dataSource: [(menu: (title: String, color: UIColor), content: ContentTableViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
let color = UIColor(
Expand All @@ -33,6 +33,13 @@ class FullscreenViewController: UIViewController {
contentViewController?.reloadData(with: 4)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
Expand Down
9 changes: 8 additions & 1 deletion iOS Sample/iOS Sample/OverlayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import PagingKit

class OverlayViewController: UIViewController {

let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let dataSource: [(menu: String, content: ContentTableViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
return (menu: title, content: vc)
Expand All @@ -47,6 +47,13 @@ class OverlayViewController: UIViewController {
})
contentViewController?.reloadData(with: 0)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
Expand Down
9 changes: 8 additions & 1 deletion iOS Sample/iOS Sample/SimpleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SimpleViewController: UIViewController {
var contentViewController: PagingContentViewController?


let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin"].map {
let dataSource: [(menu: String, content: ContentTableViewController)] = ["Martinez", "Alfred", "Louis", "Justin"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
return (menu: title, content: vc)
Expand All @@ -47,6 +47,13 @@ class SimpleViewController: UIViewController {
contentViewController?.reloadData()

}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
Expand Down
20 changes: 18 additions & 2 deletions iOS Sample/iOS Sample/TagViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class TagViewController: UIViewController {
var contentViewController: PagingContentViewController!
var menuViewController: PagingMenuViewController!

let dataSource: [(menu: (title: String, color: UIColor), content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let dataSource: [(menu: (title: String, color: UIColor), content: ContentTableViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
let color = UIColor(
Expand All @@ -49,6 +49,17 @@ class TagViewController: UIViewController {
contentViewController?.reloadData(with: 4)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
Expand All @@ -73,7 +84,8 @@ extension TagViewController: PagingContentViewControllerDataSource {
}

func contentViewController(viewController: PagingContentViewController, viewControllerAt index: Int) -> UIViewController {
return dataSource[index].content
let content = dataSource[index].content
return content
}
}

Expand Down Expand Up @@ -110,6 +122,10 @@ extension TagViewController: PagingMenuViewControllerDelegate {

contentViewController?.scroll(to: page, animated: true)
}

func menuViewController(viewController: PagingMenuViewController, focusViewDidEndTransition focusView: PagingMenuFocusView) {

}
}

extension TagViewController: PagingContentViewControllerDelegate {
Expand Down
9 changes: 8 additions & 1 deletion iOS Sample/iOS Sample/TwoLineMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TwoLineMenuViewController: UIViewController {
var contentViewController: PagingContentViewController?


let dataSource: [(menu: (title: String, subTitle: String?, isEnabledPoo: Bool), content: UIViewController)] = [(title: "Martinez", subTitle: nil, isEnabledPoo: true), (title: "Alfred", subTitle: nil, isEnabledPoo: false), (title: "Louis", subTitle: "owner", isEnabledPoo: false), (title: "Justin", subTitle: nil, isEnabledPoo: false)].map {
let dataSource: [(menu: (title: String, subTitle: String?, isEnabledPoo: Bool), content: ContentTableViewController)] = [(title: "Martinez", subTitle: nil, isEnabledPoo: true), (title: "Alfred", subTitle: nil, isEnabledPoo: false), (title: "Louis", subTitle: "owner", isEnabledPoo: false), (title: "Justin", subTitle: nil, isEnabledPoo: false)].map {
let title = $0.title
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
return (menu: $0, content: vc)
Expand All @@ -50,6 +50,13 @@ class TwoLineMenuViewController: UIViewController {
super.didReceiveMemoryWarning()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? PagingMenuViewController {
menuViewController = vc
Expand Down
9 changes: 8 additions & 1 deletion iOS Sample/iOS Sample/ViewInsertedViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ViewInsertedViewController: UIViewController {
var menuViewController: PagingMenuViewController?
var contentViewController: PagingContentViewController?

let dataSource: [(menu: String, content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin"].map {
let dataSource: [(menu: String, content: ContentTableViewController)] = ["Martinez", "Alfred", "Louis", "Justin"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
return (menu: title, content: vc)
Expand All @@ -58,6 +58,13 @@ class ViewInsertedViewController: UIViewController {
super.didReceiveMemoryWarning()
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if #available(iOS 11.0, *) {
dataSource.forEach { $1.leadingInset = view.safeAreaInsets.left }
}
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? PagingMenuViewController {
menuViewController = vc
Expand Down

0 comments on commit 6df1f47

Please sign in to comment.