-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Missing Popover Messages snippet files that weren't automatically com…
…mited by Xcode.
- Loading branch information
1 parent
b9584ad
commit da7e4f4
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
Binary file modified
BIN
-399 Bytes
(100%)
...eproj/project.xcworkspace/xcuserdata/elliottio.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
31 changes: 31 additions & 0 deletions
31
StoryboardSnippets/StoryboardSnippets/Snippets/PopoverMessage/AlwaysPresentAsPopover.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// AlwaysPresentAsPopover.swift | ||
// StoryboardSnippets | ||
// | ||
// Created by Elliott Io on 4/26/20. | ||
// Copyright © 2020 elliott io. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
class AlwaysPresentAsPopover : NSObject, UIPopoverPresentationControllerDelegate { | ||
|
||
// `sharedInstance` because the delegate property is weak - the delegate instance needs to be retained. | ||
private static let sharedInstance = AlwaysPresentAsPopover() | ||
|
||
private override init() { | ||
super.init() | ||
} | ||
|
||
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle { | ||
return .none | ||
} | ||
|
||
static func configurePresentation(forController controller : UIViewController) -> UIPopoverPresentationController { | ||
controller.modalPresentationStyle = .popover | ||
let presentationController = controller.presentationController as! UIPopoverPresentationController | ||
presentationController.delegate = AlwaysPresentAsPopover.sharedInstance | ||
return presentationController | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
StoryboardSnippets/StoryboardSnippets/Snippets/PopoverMessage/PopoverViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// | ||
// AlwaysPresentAsPopover.swift | ||
// StoryboardSnippets | ||
// | ||
// Created by Elliott Io on 4/26/20. | ||
// Copyright © 2020 elliott io. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
class PopoverViewController: UIViewController { | ||
|
||
@IBOutlet weak var labelMessage: UILabel! | ||
var message:String? | ||
var sendingViewController: UIViewController? | ||
var arrowDirection: UIPopoverArrowDirection = .any | ||
|
||
let ltPadding: CGFloat = 20 // leading/trailing padding | ||
let tbPadding: CGFloat = 14 // top/bottom padding | ||
let arrowPadding: CGFloat = 13 // popover arrow padding | ||
let dimLevel: CGFloat = 0.6 | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
if let message = self.message { | ||
self.labelMessage.text = message | ||
} | ||
let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:))) | ||
view.addGestureRecognizer(tap) | ||
|
||
// dim sendingViewController | ||
UIView.animate(withDuration: AntimationDuration.fadeInOut.rawValue, animations: { | ||
self.sendingViewController?.view.alpha = self.dimLevel | ||
}) | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
|
||
print("arrowDirection: \(arrowDirection)") | ||
|
||
let item: UIView = self.labelMessage | ||
|
||
guard let superview = item.superview else { return } | ||
|
||
// all constaints must be set for a proper display | ||
|
||
// set centered constraincts | ||
item.centerYAnchor.constraint(equalTo: superview.centerYAnchor).isActive = true | ||
item.centerXAnchor.constraint(equalTo: superview.centerXAnchor).isActive = true | ||
|
||
// iOS grows view if there is an arrow, so adjust anchors to fix offset | ||
if arrowDirection.rawValue == 2 { //.down { | ||
item.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: ltPadding).isActive = true | ||
item.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: ltPadding).isActive = true | ||
item.topAnchor.constraint(equalTo: superview.topAnchor, constant: tbPadding).isActive = true | ||
item.bottomAnchor.constraint(greaterThanOrEqualTo: superview.bottomAnchor, constant: tbPadding + arrowPadding).isActive = true | ||
item.widthAnchor.constraint(equalTo: superview.widthAnchor, constant: -(ltPadding * 2)).isActive = true | ||
item.heightAnchor.constraint(equalTo: superview.heightAnchor, constant: -(tbPadding * 2) - arrowPadding).isActive = true | ||
} else if arrowDirection.rawValue == 1 { //.up { | ||
item.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: ltPadding).isActive = true | ||
item.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: ltPadding).isActive = true | ||
item.topAnchor.constraint(equalTo: superview.topAnchor, constant: tbPadding + arrowPadding).isActive = true | ||
item.bottomAnchor.constraint(greaterThanOrEqualTo: superview.bottomAnchor, constant: tbPadding).isActive = true | ||
item.widthAnchor.constraint(equalTo: superview.widthAnchor, constant: -(ltPadding * 2)).isActive = true | ||
item.heightAnchor.constraint(equalTo: superview.heightAnchor, constant: -(tbPadding * 2) - arrowPadding).isActive = true | ||
} else if arrowDirection.rawValue == 4 { //.left { | ||
item.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: ltPadding + arrowPadding).isActive = true | ||
item.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: ltPadding).isActive = true | ||
item.topAnchor.constraint(equalTo: superview.topAnchor, constant: tbPadding).isActive = true | ||
item.bottomAnchor.constraint(greaterThanOrEqualTo: superview.bottomAnchor, constant: tbPadding).isActive = true | ||
item.widthAnchor.constraint(equalTo: superview.widthAnchor, constant: -(ltPadding * 2) - arrowPadding).isActive = true | ||
item.heightAnchor.constraint(equalTo: superview.heightAnchor, constant: -(tbPadding * 2)).isActive = true | ||
} else if arrowDirection.rawValue == 8 { //.right { | ||
item.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: ltPadding).isActive = true | ||
item.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: ltPadding + arrowPadding).isActive = true | ||
item.topAnchor.constraint(equalTo: superview.topAnchor, constant: tbPadding).isActive = true | ||
item.bottomAnchor.constraint(greaterThanOrEqualTo: superview.bottomAnchor, constant: tbPadding).isActive = true | ||
item.widthAnchor.constraint(equalTo: superview.widthAnchor, constant: -(ltPadding * 2) - arrowPadding).isActive = true | ||
item.heightAnchor.constraint(equalTo: superview.heightAnchor, constant: -(tbPadding * 2)).isActive = true | ||
} else { | ||
// center if no arrows | ||
item.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: ltPadding).isActive = true | ||
item.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: ltPadding).isActive = true | ||
item.topAnchor.constraint(equalTo: superview.topAnchor, constant: tbPadding).isActive = true | ||
item.bottomAnchor.constraint(greaterThanOrEqualTo: superview.bottomAnchor, constant: tbPadding).isActive = true | ||
item.widthAnchor.constraint(equalTo: superview.widthAnchor, constant: -(ltPadding * 2)).isActive = true | ||
item.heightAnchor.constraint(equalTo: superview.heightAnchor, constant: -(tbPadding * 2)).isActive = true | ||
} | ||
} | ||
|
||
@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) { | ||
self.dismiss(animated: true, completion: nil) | ||
} | ||
|
||
override func viewWillDisappear(_ animated: Bool) { | ||
super.viewWillDisappear(animated) | ||
UIViewController.existingPopover = nil | ||
|
||
// restore dimmed sendingViewController | ||
UIView.animate(withDuration: AntimationDuration.fadeInOut.rawValue, animations: { | ||
self.sendingViewController?.view.alpha = 1 | ||
}) | ||
} | ||
} |