From 80a68398cb98507076ef5c19ecc649537441e99b Mon Sep 17 00:00:00 2001 From: Alex Butenko <> Date: Wed, 24 Apr 2019 12:43:19 +0300 Subject: [PATCH] make sure content frame is properly laid out considering arrow --- Classes/Popover.swift | 28 +++++++++++++------ Example/Podfile.lock | 4 +-- Example/Popover/ViewController.swift | 42 +++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/Classes/Popover.swift b/Classes/Popover.swift index 6618bf4..8cab9d4 100644 --- a/Classes/Popover.swift +++ b/Classes/Popover.swift @@ -97,7 +97,25 @@ open class Popover: UIView { override open func layoutSubviews() { super.layoutSubviews() - self.contentView.frame = self.bounds + + switch popoverType { + case .up: + contentView.frame = CGRect(origin: CGPoint(x: 0, y: 0), + size: CGSize(width: bounds.width, + height: bounds.height - arrowSize.height)) + case .down, .auto: + contentView.frame = CGRect(origin: CGPoint(x: 0, y: arrowSize.height), + size: CGSize(width: bounds.width, + height: bounds.height - arrowSize.height)) + case .left: + contentView.frame = CGRect(origin: CGPoint(x: 0, y: 0), + size: CGSize(width: bounds.width - arrowSize.height, + height: bounds.height)) + case .right: + contentView.frame = CGRect(origin: CGPoint(x: arrowSize.height, y: 0), + size: CGSize(width: bounds.width - arrowSize.height, + height: bounds.height)) + } } open func showAsDialog(_ contentView: UIView) { @@ -588,14 +606,6 @@ private extension Popover { func show() { self.setNeedsDisplay() - switch self.popoverType { - case .up: - self.contentView.frame.origin.y = 0.0 - case .down, .auto: - self.contentView.frame.origin.y = self.arrowSize.height - case .left, .right: - self.contentView.frame.origin.x = 0 - } self.addSubview(self.contentView) self.containerView.addSubview(self) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 5b79e49..a5ee7bb 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,5 +1,5 @@ PODS: - - Popover (1.2.1) + - Popover (1.2.2) DEPENDENCIES: - Popover (from `../`) @@ -9,7 +9,7 @@ EXTERNAL SOURCES: :path: "../" SPEC CHECKSUMS: - Popover: 31a7e57c161ea6cc86dcba9ba45a2a9371398df0 + Popover: 38d706f2f2a3e5d485851b3f2a612ab8c20f2236 PODFILE CHECKSUM: 6d2f20b0ca605fb4d048b05500bed6d649d9a399 diff --git a/Example/Popover/ViewController.swift b/Example/Popover/ViewController.swift index a5ec28c..5c28be4 100644 --- a/Example/Popover/ViewController.swift +++ b/Example/Popover/ViewController.swift @@ -9,6 +9,38 @@ import UIKit import Popover +class DetailsViewController: UIViewController { + override func viewDidLoad() { + super.viewDidLoad() + + view.backgroundColor = .white + + let label = UILabel() + label.translatesAutoresizingMaskIntoConstraints = false + label.textColor = .black + label.text = "Welcome!" + + view.addSubview(label) + + NSLayoutConstraint.activate([ + NSLayoutConstraint(item: label, + attribute: .centerX, + relatedBy: .equal, + toItem: view, + attribute: .centerX, + multiplier: 1.0, + constant: 0.0), + NSLayoutConstraint(item: label, + attribute: .centerY, + relatedBy: .equal, + toItem: view, + attribute: .centerY, + multiplier: 1.0, + constant: 0.0), + ]) + } +} + class ViewController: UIViewController { @IBOutlet weak var rightBarButton: UIBarButtonItem! @@ -67,18 +99,20 @@ class ViewController: UIViewController { @IBAction func tappedLeftTopButton(_ sender: UIButton) { let width = self.view.frame.width / 4 - let aView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: width)) + let detailsViewController = DetailsViewController() + detailsViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: width) let options: [PopoverOption] = [.type(.right), .showBlackOverlay(false)] let popover = Popover(options: options, showHandler: nil, dismissHandler: nil) - popover.show(aView, fromView: self.leftTopButton) + popover.show(detailsViewController.view, fromView: self.leftTopButton, inView: view) } @IBAction func tappedRightCenterButton(_ sender: UIButton) { let width = self.view.frame.width / 4 - let aView = UIView(frame: CGRect(x: 0, y: 0, width: width, height: width)) + let detailsViewController = DetailsViewController() + detailsViewController.view.frame = CGRect(x: 0, y: 0, width: width, height: width) let options: [PopoverOption] = [.type(.left), .showBlackOverlay(false)] let popover = Popover(options: options, showHandler: nil, dismissHandler: nil) - popover.show(aView, fromView: self.rightCenterButton) + popover.show(detailsViewController.view, fromView: self.rightCenterButton, inView: view) } }