Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make sure content frame is properly laid out considering arrow #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions Classes/Popover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Popover (1.2.1)
- Popover (1.2.2)

DEPENDENCIES:
- Popover (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Popover: 31a7e57c161ea6cc86dcba9ba45a2a9371398df0
Popover: 38d706f2f2a3e5d485851b3f2a612ab8c20f2236

PODFILE CHECKSUM: 6d2f20b0ca605fb4d048b05500bed6d649d9a399

Expand Down
42 changes: 38 additions & 4 deletions Example/Popover/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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)
}
}

Expand Down