Skip to content

Commit

Permalink
feature: support siwft 5
Browse files Browse the repository at this point in the history
  • Loading branch information
suricforever committed Apr 12, 2019
1 parent 190c28e commit d7bbac7
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 99 deletions.
14 changes: 8 additions & 6 deletions DropdownMenu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,24 @@
ACE6DB6B1CF74183005C6667 = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = 7NLBE99QC4;
LastSwiftMigration = 1000;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
ACE6DB751CF74183005C6667 = {
CreatedOnToolsVersion = 7.3.1;
DevelopmentTeam = 7NLBE99QC4;
LastSwiftMigration = 1020;
ProvisioningStyle = Automatic;
};
};
};
buildConfigurationList = ACE6DB661CF74183005C6667 /* Build configuration list for PBXProject "DropdownMenu" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = ACE6DB621CF74183005C6667;
productRefGroup = ACE6DB6D1CF74183005C6667 /* Products */;
Expand Down Expand Up @@ -369,7 +371,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -390,7 +392,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.teambition.DropdownMenu;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -403,7 +405,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.teambition.DropdownMenuTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -416,7 +418,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.teambition.DropdownMenuTests;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Binary file not shown.

This file was deleted.

This file was deleted.

48 changes: 26 additions & 22 deletions DropdownMenu/DropUpMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@ public extension DropUpMenuDelegate {
private let screenRect = UIScreen.main.bounds

open class DropUpMenu: UIView {
fileprivate var items: [DropdownItem] = []
fileprivate var selectedRow: Int
open var tableView: UITableView!
fileprivate var barCoverView: UIView!
fileprivate var isShow = false
fileprivate var addedWindow: UIWindow?
fileprivate var windowRootView: UIView?
fileprivate lazy var tapGestureRecognizer: UITapGestureRecognizer = {
private var items: [DropdownItem] = []
private var selectedRow: Int
open lazy var tableView: UITableView = {
let tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView.estimatedSectionHeaderHeight = 0
tableView.estimatedSectionFooterHeight = 0
return tableView
}()
private lazy var barCoverView: UIView = {
let barCoverView = UIView()
barCoverView.backgroundColor = UIColor.clear
barCoverView.translatesAutoresizingMaskIntoConstraints = false
return barCoverView
}()
private var isShow = false
private var addedWindow: UIWindow?
private var windowRootView: UIView?
private lazy var tapGestureRecognizer: UITapGestureRecognizer = {
return UITapGestureRecognizer(target: self, action: #selector(self.hideMenu))
}()

Expand Down Expand Up @@ -86,10 +96,10 @@ open class DropUpMenu: UIView {

clipsToBounds = true
setupGestureView()
initTableView()
setupTableView()
}

fileprivate func setupGestureView() {
private func setupGestureView() {
let gestureView = UIView()
gestureView.backgroundColor = UIColor.clear
addSubview(gestureView)
Expand All @@ -102,16 +112,13 @@ open class DropUpMenu: UIView {
gestureView.addGestureRecognizer(tapGestureRecognizer)
}

fileprivate func initTableView() {
tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView?.delegate = self
tableView?.dataSource = self
tableView.estimatedSectionHeaderHeight = 0
tableView.estimatedSectionFooterHeight = 0
private func setupTableView() {
tableView.delegate = self
tableView.dataSource = self
addSubview(tableView)
}

fileprivate func layoutTableView() {
private func layoutTableView() {
tableViewHeight = CGFloat(items.count) * rowHeight
let maxHeight = UIScreen.main.bounds.height - bottomOffsetY
if tableViewHeight > maxHeight {
Expand All @@ -125,7 +132,7 @@ open class DropUpMenu: UIView {
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
}

fileprivate func setupBottomSeperatorView() {
private func setupBottomSeperatorView() {
let seperatorView = UIView()
seperatorView.backgroundColor = tableViewSeperatorColor
addSubview(seperatorView)
Expand All @@ -136,10 +143,7 @@ open class DropUpMenu: UIView {
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
}

fileprivate func setupBottomCoverView(on view: UIView) {
barCoverView = UIView()
barCoverView.backgroundColor = UIColor.clear
barCoverView.translatesAutoresizingMaskIntoConstraints = false
private func setupBottomCoverView(on view: UIView) {
view.addSubview(barCoverView)
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: bottomOffsetY)])
Expand Down
56 changes: 29 additions & 27 deletions DropdownMenu/DropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,28 @@ public extension DropdownMenuDelegate {
}

open class DropdownMenu: UIView {
fileprivate weak var navigationController: UINavigationController!
private weak var navigationController: UINavigationController!

fileprivate var sections: [DropdownSection] = []
fileprivate var selectedIndexPath: IndexPath
private var sections: [DropdownSection] = []
private var selectedIndexPath: IndexPath

open var tableView: UITableView!
fileprivate var barCoverView: UIView?
open var tableView: UITableView = {
let tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView.estimatedSectionFooterHeight = 0
tableView.estimatedSectionHeaderHeight = 0
return tableView
}()
private var barCoverView: UIView?
open var isShow = false
fileprivate var addedWindow: UIWindow?
fileprivate var windowRootView: UIView?
fileprivate var topConstraint: NSLayoutConstraint?
fileprivate var navigationBarCoverViewHeightConstraint: NSLayoutConstraint?
fileprivate var tableViewHeightConstraint: NSLayoutConstraint?
fileprivate let iPhoneXPortraitTopOffset: CGFloat = 88.0
fileprivate let portraitTopOffset: CGFloat = 64.0
fileprivate let landscapeTopOffset: CGFloat = 32.0
fileprivate var topLayoutConstraintConstant: CGFloat {
private var addedWindow: UIWindow?
private var windowRootView: UIView?
private var topConstraint: NSLayoutConstraint?
private var navigationBarCoverViewHeightConstraint: NSLayoutConstraint?
private var tableViewHeightConstraint: NSLayoutConstraint?
private let iPhoneXPortraitTopOffset: CGFloat = 88.0
private let portraitTopOffset: CGFloat = 64.0
private let landscapeTopOffset: CGFloat = 32.0
private var topLayoutConstraintConstant: CGFloat {
var offset: CGFloat = 0
if !navigationController.isNavigationBarHidden {
offset = navigationController.navigationBar.frame.height + navigationController.navigationBar.frame.origin.y
Expand Down Expand Up @@ -108,7 +113,7 @@ open class DropdownMenu: UIView {

clipsToBounds = true
setupGestureView()
initTableView()
setupTableView()

NotificationCenter.default.addObserver(self, selector: #selector(self.updateForOrientationChange(_:)), name: UIApplication.willChangeStatusBarOrientationNotification, object: nil)
}
Expand All @@ -123,7 +128,7 @@ open class DropdownMenu: UIView {

clipsToBounds = true
setupGestureView()
initTableView()
setupTableView()

NotificationCenter.default.addObserver(self, selector: #selector(self.updateForOrientationChange(_:)), name: UIApplication.willChangeStatusBarOrientationNotification, object: nil)
}
Expand Down Expand Up @@ -186,7 +191,7 @@ open class DropdownMenu: UIView {
}
}

fileprivate func setupGestureView() {
private func setupGestureView() {
let gestureView = UIView()
gestureView.backgroundColor = UIColor.clear
addSubview(gestureView)
Expand All @@ -199,17 +204,14 @@ open class DropdownMenu: UIView {
gestureView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideMenu)))
}

fileprivate func initTableView() {
tableView = UITableView(frame: CGRect.zero, style: .grouped)
private func setupTableView() {
tableView.separatorStyle = separatorStyle
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedSectionFooterHeight = 0
tableView.estimatedSectionHeaderHeight = 0
addSubview(tableView)
}

fileprivate func layoutTableView() {
private func layoutTableView() {
tableView.translatesAutoresizingMaskIntoConstraints = false

updateTableViewHeight()
Expand All @@ -222,7 +224,7 @@ open class DropdownMenu: UIView {
self.tableViewHeightConstraint = tableViewHeightConstraint
}

fileprivate func updateTableViewHeight() {
private func updateTableViewHeight() {
tableViewHeight = tableviewHeight()

let maxHeight = navigationController.view.frame.height - topLayoutConstraintConstant - defaultBottonMargin
Expand All @@ -236,7 +238,7 @@ open class DropdownMenu: UIView {
}
}

fileprivate func updateForSectionsChange(_ animations: (() -> Void)? = nil) {
private func updateForSectionsChange(_ animations: (() -> Void)? = nil) {
updateTableViewHeight()
tableViewHeightConstraint?.constant = tableViewHeight
UIView.animate(withDuration: 0.3) { [weak self] in
Expand All @@ -248,7 +250,7 @@ open class DropdownMenu: UIView {
}
}

fileprivate func setupTopSeperatorView() {
private func setupTopSeperatorView() {
let seperatorView = UIView()
seperatorView.backgroundColor = tableViewSeperatorColor
addSubview(seperatorView)
Expand All @@ -259,7 +261,7 @@ open class DropdownMenu: UIView {
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
}

fileprivate func setupNavigationBarCoverView(on view: UIView) {
private func setupNavigationBarCoverView(on view: UIView) {
barCoverView = UIView()
barCoverView?.backgroundColor = UIColor.clear
view.addSubview(barCoverView!)
Expand All @@ -273,7 +275,7 @@ open class DropdownMenu: UIView {
barCoverView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideMenu)))
}

fileprivate func tableviewHeight() -> CGFloat {
private func tableviewHeight() -> CGFloat {
var height: CGFloat = 0
sections.enumerated().forEach {
if displaySectionHeader {
Expand Down
9 changes: 6 additions & 3 deletions DropdownMenu/SectionHeader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
//

open class SectionHeader: UIView {
var titleLabel: UILabel!
var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
return titleLabel
}()

var style: SectionHeaderStyle = SectionHeaderStyle()

convenience init(style: SectionHeaderStyle) {
Expand All @@ -25,8 +30,6 @@ open class SectionHeader: UIView {
}

func commonInit() {
titleLabel = UILabel()
titleLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.font = style.font
titleLabel.textColor = style.textColor
backgroundColor = style.backgroundColor
Expand Down
Loading

0 comments on commit d7bbac7

Please sign in to comment.