Skip to content

Commit

Permalink
Support safe area layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yysskk committed Oct 22, 2017
1 parent 42807ad commit 1433d71
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0
3.2
24 changes: 12 additions & 12 deletions Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12089"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -21,12 +22,11 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" showsTouchWhenHighlighted="YES" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="g7u-0R-yRV">
<rect key="frame" x="295" y="587" width="100" height="100"/>
<rect key="frame" x="279" y="577" width="80" height="80"/>
<constraints>
<constraint firstAttribute="width" constant="100" id="OnO-dz-8IR"/>
<constraint firstAttribute="height" constant="100" id="Vbe-o1-8WM"/>
<constraint firstAttribute="width" constant="80" id="OnO-dz-8IR"/>
<constraint firstAttribute="height" constant="80" id="Vbe-o1-8WM"/>
</constraints>
<inset key="imageEdgeInsets" minX="20" minY="20" maxX="30" maxY="30"/>
<state key="normal" image="settings">
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
Expand All @@ -35,7 +35,7 @@
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="50"/>
<real key="value" value="40"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="borderColor">
<color key="value" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
Expand All @@ -48,8 +48,8 @@
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="g7u-0R-yRV" secondAttribute="bottom" constant="-20" id="4ms-kt-bai"/>
<constraint firstAttribute="trailing" secondItem="g7u-0R-yRV" secondAttribute="trailing" constant="-20" id="fVn-35-H4z"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="g7u-0R-yRV" secondAttribute="bottom" constant="10" id="4ms-kt-bai"/>
<constraint firstAttribute="trailing" secondItem="g7u-0R-yRV" secondAttribute="trailing" constant="16" id="fVn-35-H4z"/>
</constraints>
</view>
<connections>
Expand Down Expand Up @@ -101,7 +101,7 @@
</connections>
</stepper>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Tab Margin" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fsO-V6-A6a">
<rect key="frame" x="118.5" y="66" width="67.5" height="30"/>
<rect key="frame" x="118.5" y="66" width="67" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="Ooj-pU-xRe"/>
</constraints>
Expand Down Expand Up @@ -166,7 +166,7 @@
</connections>
</segmentedControl>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Adjust Tab Item Width" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="avh-1H-pWf">
<rect key="frame" x="85" y="264" width="134" height="30"/>
<rect key="frame" x="85.5" y="264" width="133.5" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="IRM-l3-5As"/>
</constraints>
Expand All @@ -189,7 +189,7 @@
</connections>
</segmentedControl>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Tab Item Width: 100" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="3HF-bf-90C">
<rect key="frame" x="91.5" y="330" width="121.5" height="30"/>
<rect key="frame" x="91.5" y="330" width="121" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="x5w-fL-zWC"/>
</constraints>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

[![Platform](http://img.shields.io/badge/platform-iOS-blue.svg?style=flat
)](https://developer.apple.com/iphone/index.action)
![Swift](https://img.shields.io/badge/Swift-3.0-orange.svg)
![Swift](https://img.shields.io/badge/Swift-3.2-orange.svg)
[![Cocoapods](https://img.shields.io/badge/Cocoapods-compatible-brightgreen.svg)](https://img.shields.io/badge/Cocoapods-compatible-brightgreen.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-Compatible-brightgreen.svg?style=flat)](https://github.com/Carthage/Carthage)
[![License](http://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat
)](http://mit-license.org)
![pod](https://img.shields.io/badge/pod-v1.1.5-red.svg)
![pod](https://img.shields.io/badge/pod-v1.2.0-red.svg)

## Overview
SwipeMenuViewController provides `SwipeMenuView` and `SwipeMenuViewController`.
Expand Down
8 changes: 5 additions & 3 deletions Sources/ContentScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ open class ContentScrollView: UIScrollView {

currentIndex = defaultIndex

if #available(iOS 11.0, *) {
self.contentInsetAdjustmentBehavior = .never
}

if let options = options {
self.options = options
}
Expand Down Expand Up @@ -106,9 +110,7 @@ open class ContentScrollView: UIScrollView {
])
}

guard currentIndex < dataSource.numberOfPages(in: self) else {
return
}
guard currentIndex < dataSource.numberOfPages(in: self) else { return }
for i in (currentIndex + 1)..<dataSource.numberOfPages(in: self) {
guard let pageView = dataSource.contentScrollView(self, viewForPageAt: i) else { return }
pageViews.append(pageView)
Expand Down
17 changes: 17 additions & 0 deletions Sources/Extensions/UIViewExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// UIViewExtension.swift
// SwipeMenuViewController
//
// Created by 森下 侑亮 on 2017/10/22.
// Copyright © 2017年 yysskk. All rights reserved.
//

import UIKit

extension UIView {

var hasSafeAreaInsets: Bool {
guard #available (iOS 11, *) else { return false }
return safeAreaInsets != .zero
}
}
23 changes: 18 additions & 5 deletions Sources/SwipeMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public struct SwipeMenuViewOptions {
public var addition: Addition = .underline
public var needsAdjustItemViewWidth: Bool = true
public var needsConvertTextColorRatio: Bool = true
public var isSafeAreaEnabled: Bool = true

// item
public var itemView = ItemView()
Expand All @@ -52,6 +53,15 @@ public struct SwipeMenuViewOptions {
// self
public var backgroundColor: UIColor = .clear
public var isScrollEnabled: Bool = true
public var isSafeAreaEnabled: Bool = true
}

// self
public var isSafeAreaEnabled: Bool = true {
didSet {
tabView.isSafeAreaEnabled = isSafeAreaEnabled
contentScrollView.isSafeAreaEnabled = isSafeAreaEnabled
}
}

// TabView
Expand Down Expand Up @@ -152,7 +162,6 @@ open class SwipeMenuView: UIView {

isLayoutingSubviews = true
super.layoutSubviews()

reloadData(isOrientationChange: true)
}

Expand All @@ -168,19 +177,19 @@ open class SwipeMenuView: UIView {
self.options = options
}

self.isLayoutingSubviews = isOrientationChange
isLayoutingSubviews = isOrientationChange

if !isOrientationChange {
if !isLayoutingSubviews {
reset()
setup(default: defaultIndex ?? currentIndex)
}

jump(to: defaultIndex ?? currentIndex, animated: false)

self.isLayoutingSubviews = false
isLayoutingSubviews = false
}

public func jump(to index: Int, animated: Bool) {
public func jump(to index: Int, animated: Bool) {

if let tabView = tabView, let contentScrollView = contentScrollView {
tabView.jump(to: index)
Expand Down Expand Up @@ -219,6 +228,10 @@ open class SwipeMenuView: UIView {

contentScrollView = ContentScrollView(frame: CGRect(x: 0, y: options.tabView.height, width: frame.width, height: frame.height - options.tabView.height), default: defaultIndex, options: options.contentScrollView)

tabView?.update(defaultIndex)
contentScrollView?.update(defaultIndex)
currentIndex = defaultIndex

delegate?.swipeMenuView(self, viewDidSetupAt: defaultIndex)
}

Expand Down
25 changes: 19 additions & 6 deletions Sources/SwipeMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ open class SwipeMenuViewController: UIViewController, SwipeMenuViewDelegate, Swi
swipeMenuView.delegate = self
swipeMenuView.dataSource = self
view.addSubview(swipeMenuView)
addSwipeMenuViewConstraints()
}

open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
Expand All @@ -20,16 +19,30 @@ open class SwipeMenuViewController: UIViewController, SwipeMenuViewDelegate, Swi
swipeMenuView.willChangeOrientation()
}

open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
addSwipeMenuViewConstraints()
}

private func addSwipeMenuViewConstraints() {

swipeMenuView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
swipeMenuView.topAnchor.constraint(equalTo: self.view.topAnchor),
swipeMenuView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
swipeMenuView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
swipeMenuView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
if #available(iOS 11.0, *), view.hasSafeAreaInsets, swipeMenuView.options.tabView.isSafeAreaEnabled {
NSLayoutConstraint.activate([
swipeMenuView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
swipeMenuView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
swipeMenuView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
swipeMenuView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
} else {
NSLayoutConstraint.activate([
swipeMenuView.topAnchor.constraint(equalTo: self.view.topAnchor),
swipeMenuView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
swipeMenuView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
swipeMenuView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
}
}

// MARK: - SwipeMenuViewDelegate
Expand Down
2 changes: 1 addition & 1 deletion Sources/TabItemView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import UIKit

class TabItemView: UIView {
final class TabItemView: UIView {

private(set) var titleLabel: UILabel = UILabel()

Expand Down
Loading

0 comments on commit 1433d71

Please sign in to comment.