Apply Rotation to popup - swift

I am using PopupDialog which is a subclass of UIViewController and would like to force the rotation of just this view upon popup.
here is an example of what I would like to do:
import UIKit
import PopupDialog
class TestViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func presentpopup() {
let popup = PopupDialog(title: "Title", message: "My message", image: nil)
let buttonOne = CancelButton(title: "cancel") {}
let buttonTwo = DefaultButton(title: "go to settings", dismissOnTap: false){
print("pressed")
}
//****** Here is where I would like to rotate the entire view***/////
popup.addButtons([buttonOne, buttonTwo])
self.present(popup, animated: true, completion: nil)
}
}
What I would like is something along the line of popup.rotate(.pi/2). Is this possible? I can't Seem to figure out how.

popup.view.transform = CGAffineTransform(rotationAngle: -.pi/2)
This rotates the view 90 degrees. it can be applied to any view.

Related

How can i create a searchbar like safari's searchbar ? SWIFT

How can i create a searchbar like safari
Just like the photos I posted
That the searchbar becomes small or hidden when the screen is scrolled
...
PHOTOS
It would be great if you could tell, what you've tried before.
That way we can help you better.
In order to do that, you need a view of type UIScrollView (UITableView, UICollectionView, etc.).
Implement this delegate method and it should do the trick.
Adjust the swipeThreshold variable to fit your needs.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let swipeThreshold: CGFloat = 10
let y = scrollView.panGestureRecognizer.translation(in: scrollView).y
if y != 0 && swipeThreshold > y {
navigationController?.setNavigationBarHidden(true, animated: true)
} else {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
Old answer as reference:
Here is one way to do it — by using SFSafariViewController using SafariServices.
import UIKit
import SafariServices
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
// Call this method when you want to show the browser
func doSomething() {
let url = URL(string: "https://www.google.com")!
let safariViewController = SFSafariViewController(url: url)
present(safariViewController, animated: true, completion: nil)
}
}

Return control to function when modal viewcontroller dismissed

I need to present a modal VC that sets a property in my presenting VC, and then I need to do something with that value back in the presenting VC. I have to be able to pass pointers to different properties to this function, so that it's reusable. I have the code below (KeyPickerTableViewController is the modal VC).
It should work, except not, because the line after present(picker... gets executed immediately after the picker is presented.
How do I get my presenting VC to "wait" until the modal VC is dismissed?
#objc func fromKeyTapped(_ button: UIBarButtonItem) {
print("from tapped")
setKey(for: &sourceKey, presentingFrom: button)
}
#objc func toKeyTapped(_ button: UIBarButtonItem) {
print("from tapped")
setKey(for: &destKey, presentingFrom: button)
}
fileprivate func setKey(for key: inout Key!, presentingFrom buttonItem: UIBarButtonItem) {
let picker = KeyPickerTableViewController()
picker.delegate = self
picker.modalPresentationStyle = .popover
picker.popoverPresentationController?.barButtonItem = buttonItem
present(picker, animated: true, completion: nil)
if let delKey = delegatedKey {
key = delKey
}
}
You could use delegate pattern or closure.
I would do the following
1. I would not use inout pattern, I would first call the popover and then separately update what is needed to be updated
2. In KeyPickerTableViewController define property var actionOnDismiss: (()->())? and setting this action to what we need after initialisation of KeyPickerTableViewController
I could show it in code, but the abstract you've shown is not clear enough to come up with specific amendments. Please refer the illustration below.
import UIKit
class FirstVC: UIViewController {
var key = 0
#IBAction func buttonPressed(_ sender: Any) {
let vc = SecondVC()
vc.action = {
print(self.key)
self.key += 1
print(self.key)
}
present(vc, animated: true, completion: nil)
}
}
class SecondVC: UIViewController {
var action: (()->())?
override func viewDidLoad() {
onDismiss()
}
func onDismiss() {
action?()
}
}
While presenting VC, add dismissing modal VC action in its completion handler, so that Viewcontroller will be presented after dismissal is completed
present(picker, animated: true, completion: { (action) in
//dismissal action
if let delKey = delegatedKey {
key = delKey
}
})

swift mac osx NSButton is not responding until a long press

I am having a weird issue with a button. So I have a NSViewController with many subviews in it. When I click a button, a new NSView with click gestures and buttons is added on top. But I can't press any of them, they don't respond unless a click for 2 seconds and then release. I've tried disabling the gestures of the holder but it didn't work. Any suggestions?
Well, some of the rest of us do. In my case, it's for buttons on a view in a sheet, so "many subviews" isn't likely it. My view controller for the sheet is about 100 lines. Still debugging...
At present the VC is as follows. The snp.makeConstraints calls are for SnapKit (from GitHub)
#objc
class ThreadEditSheetViewController: NSViewController {
/// The container for the graphics view
#IBOutlet var sheetView: NSView!
/// The information packet initialized by the invoking view controller
var info: ThreadEditInfo!
/// API
override func viewDidLoad() {
super.viewDidLoad()
}
/// API
override func viewWillAppear() {
guard let gvc = (try? self.bundleLoader(id: "GraphicsViewController")) as? GraphicsViewController else {
fatalUserAlert(error: AppError.UIConstructionFailure, message: "Can't find GraphicsViewController for ThreadEditSheetViewController")}
let gv = gvc.view
self.view.addSubview(gv)
// Spaces in title text move it left to avoid visual overlap with scroll bar. Don't know how to do it with
// constraints given the scrolling view
let done = makeButton(gvc: gvc, title: "done ", action: #selector(doneEditing(_:)))
done.snp.makeConstraints{ (make) in
make.top.equalTo(gv).offset(-5)
make.right.equalTo(gv).offset(-5)
}
let cancel = makeButton(gvc: gvc, title: "cancel", action: #selector(cancelEditing(_:)))
cancel.snp.makeConstraints{ (make) in
make.top.equalTo(gv).offset(-5)
make.left.equalTo(gv).offset(5)
}
self.view.becomeFirstResponder()
super.viewWillAppear()
return
}
func makeButton(gvc: NSViewController, title: String, action: Selector) -> NSButton {
let button = NSButton(title: title, target: self, action: action)
let gv = gvc.view
gv.addSubview(button)
button.backgroundColor = .clear
button.setButtonType(.momentaryChange)
button.isTransparent = true
return button
}
#objc
func doneEditing(_ sender: Any) {
self.dismissViewController(self)
}
#objc
func cancelEditing(_ sender: Any) {
self.dismissViewController(self)
}
}

Popup window in OS X App

I have a little problem with app and looking for the best solution. I want to create an popup window that have separate logic in other NSViewController.
The case is - I click on the button in my window and then other window appears with some NSTextField's and custom design. I'm considering what component is the best for my issue.
something like this:
my view
Thank you in advance for any help !
If have done it with the following code:
let addMyPopover = NSPopover()
if let popOverController = aViewController(xyz: data) {
popOverController.closeDelegate = self
addMyPopover.behavior = .Transient
addMyPopover.contentViewController = popOverController
addMyPopover.contentSize = CGSizeMake(450, 380)
addMyPopover.showRelativeToRect(addButton.bounds, ofView: addPhaseButton, preferredEdge: NSRectEdge.MinY)
}
extension MyController: closeViewDelegate {
func closePopover(sender: AnyObject?) {
addMyPopover.performClose(sender)
}
}
protocol closeViewDelegate: class {
func closePopover(sender: AnyObject?)
}
aViewController is from type NSViewController
#IBAction func showPopover(_ sender: NSButton) {
let vcPopover = NSStoryboard(name: "Main", bundle: nil).instantiateController(withIdentifier: "viewControllerPop") as! NSViewController
self.presentViewController(vcPopover, asPopoverRelativeTo : sender.bounds, of : sender, preferredEdge: .maxY, behavior: .transient)
}

How to dismiss view in swift? dismissViewControllerAnimated is not doing anything?

I have an issue where dismissViewController method is not actually doing anything. My code is as follows:
import UIKit
class CameraController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
....
#IBAction func dismissPhotoPicker(sender: UIButton!) {
print("in the function")
self.dismissViewControllerAnimated(true, completion: nil)
}
}
No errors are thrown when running, click on the button does trigger the method (I put a print statement in the function)
There are 2 buttons "Back", which calls that method, and another one called "Open Image Picker", which opens the Image Picker.
My storyboard starts with a TabBarViewController, I set up a segue such that when clicking on the "Camera" button, it switches to the "CameraViewController", which is associated with the custom class CameraController (which I've specified in my Storyboard.xib file)
Can you do something like this?
yourView:UIView {
// do something
}
yourMainController:UIViewController {
let view = yourView()
func showView() {
self.view.addSubview(view)
}
func hideView() {
view.removeFromSuperView()
}
}
Try this code:
if self.isBeingPresented() {
// being presented
print("being present, going to dismiss..")
self.dismissViewControllerAnimated(true, completion: nil)
} else if self.isMovingToParentViewController() {
// being pushed
print("being pushed, going to pop..")
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
} else {
// simply showing again because another VC was dismissed
}