diff --git a/DropdownMenu/DropUpMenu.swift b/DropdownMenu/DropUpMenu.swift index d470c3e..0414df0 100644 --- a/DropdownMenu/DropUpMenu.swift +++ b/DropdownMenu/DropUpMenu.swift @@ -86,6 +86,7 @@ open class DropUpMenu: UIView { clipsToBounds = true setupGestureView() + initTableView() } fileprivate func setupGestureView() { @@ -101,28 +102,20 @@ 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)]) @@ -130,6 +123,17 @@ 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() { + 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 @@ -150,7 +154,7 @@ open class DropUpMenu: UIView { } isShow = true - setupTableView() + layoutTableView() setupBottomSeperatorView() if let rootView = UIApplication.shared.keyWindow { diff --git a/DropdownMenu/DropdownMenu.swift b/DropdownMenu/DropdownMenu.swift index 6df2d05..05b5be4 100644 --- a/DropdownMenu/DropdownMenu.swift +++ b/DropdownMenu/DropdownMenu.swift @@ -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) } @@ -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) } @@ -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 @@ -223,7 +229,7 @@ open class DropdownMenu: UIView { isShow = true - setupTableView() + layoutTableView() setupTopSeperatorView() if let rootView = UIApplication.shared.keyWindow { diff --git a/DropdownMenuDemo/DropdownMenuDemo/ViewController.swift b/DropdownMenuDemo/DropdownMenuDemo/ViewController.swift index 4dda3fa..44d390e 100644 --- a/DropdownMenuDemo/DropdownMenuDemo/ViewController.swift +++ b/DropdownMenuDemo/DropdownMenuDemo/ViewController.swift @@ -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() }