Skip to content

Commit

Permalink
fix crash when set tableview seperatorStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Suric zhang committed Aug 16, 2017
1 parent 6f2f7a5 commit 8e89a67
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 33 deletions.
34 changes: 19 additions & 15 deletions DropdownMenu/DropUpMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ open class DropUpMenu: UIView {

clipsToBounds = true
setupGestureView()
initTableView()
}

fileprivate func setupGestureView() {
Expand All @@ -101,35 +102,38 @@ open class DropUpMenu: UIView {
gestureView.addGestureRecognizer(tapGestureRecognizer)
}

fileprivate func setupBottomSeperatorView() {
let seperatorView = UIView()
seperatorView.backgroundColor = tableViewSeperatorColor
addSubview(seperatorView)
seperatorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
fileprivate func initTableView() {
tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView?.delegate = self
tableView?.dataSource = self
addSubview(tableView)
}

fileprivate func setupTableView() {
fileprivate func layoutTableView() {
tableViewHeight = CGFloat(items.count) * rowHeight
let maxHeight = UIScreen.main.bounds.height - bottomOffsetY
if tableViewHeight > maxHeight {
tableViewHeight = maxHeight
}

tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView?.delegate = self
tableView?.dataSource = self
addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant:0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: tableViewHeight)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
}

fileprivate func setupBottomSeperatorView() {
let seperatorView = UIView()
seperatorView.backgroundColor = tableViewSeperatorColor
addSubview(seperatorView)
seperatorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
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
Expand All @@ -150,7 +154,7 @@ open class DropUpMenu: UIView {
}
isShow = true

setupTableView()
layoutTableView()
setupBottomSeperatorView()

if let rootView = UIApplication.shared.keyWindow {
Expand Down
40 changes: 23 additions & 17 deletions DropdownMenu/DropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ open class DropdownMenu: UIView {

clipsToBounds = true
setupGestureView()
initTableView()

NotificationCenter.default.addObserver(self, selector: #selector(self.updateForOrientationChange(_:)), name: NSNotification.Name.UIApplicationWillChangeStatusBarOrientation, object: nil)
}
Expand All @@ -115,6 +116,7 @@ open class DropdownMenu: UIView {

clipsToBounds = true
setupGestureView()
initTableView()

NotificationCenter.default.addObserver(self, selector: #selector(self.updateForOrientationChange(_:)), name: NSNotification.Name.UIApplicationWillChangeStatusBarOrientation, object: nil)
}
Expand Down Expand Up @@ -159,36 +161,40 @@ open class DropdownMenu: UIView {
gestureView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideMenu)))
}

fileprivate func setupTopSeperatorView() {
let seperatorView = UIView()
seperatorView.backgroundColor = tableViewSeperatorColor
addSubview(seperatorView)
seperatorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 0.5)])
fileprivate func initTableView() {
tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView.separatorStyle = separatorStyle
tableView?.delegate = self
tableView?.dataSource = self
addSubview(tableView)
}

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

tableViewHeight = tableviewHeight()
let maxHeight = navigationController.view.frame.height - topLayoutConstraintConstant - defaultBottonMargin
if tableViewHeight > maxHeight {
tableViewHeight = maxHeight
}

tableView = UITableView(frame: CGRect.zero, style: .grouped)
tableView.separatorStyle = separatorStyle
tableView?.delegate = self
tableView?.dataSource = self
addSubview(tableView)
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant:0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: tableViewHeight)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: tableView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
}

fileprivate func setupTopSeperatorView() {
let seperatorView = UIView()
seperatorView.backgroundColor = tableViewSeperatorColor
addSubview(seperatorView)
seperatorView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: seperatorView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0)])
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) {
barCoverView = UIView()
barCoverView?.backgroundColor = UIColor.clear
Expand Down Expand Up @@ -223,7 +229,7 @@ open class DropdownMenu: UIView {

isShow = true

setupTableView()
layoutTableView()
setupTopSeperatorView()

if let rootView = UIApplication.shared.keyWindow {
Expand Down
3 changes: 2 additions & 1 deletion DropdownMenuDemo/DropdownMenuDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class ViewController: UIViewController {
menuView = DropdownMenu(navigationController: navigationController!, items: [item1, item2, item3, item4], selectedRow: selectedRow)
}
menuView?.topOffsetY = CGFloat(offSetSlider.value)
menuView?.separatorStyle = .none
menuView?.delegate = self
menuView?.rowHeight = 70
menuView?.rowHeight = 50
menuView?.showMenu()
}

Expand Down

0 comments on commit 8e89a67

Please sign in to comment.