How to take photo automatically by handling the shutter button - swift3

understand iphone has the auto taking photo feature by setting the timer. after this press the shutter button to start.
I want to do something similar. So How to handle the Shutter Button programmatically after the camera has launched.
- implement: UINavigationControllerDelegate, UIImagePickerControllerDelegate
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
var imagePicker: UIImagePickerController!
}
//-- launch the camera
#IBAction func takePhoto(_ sender: UIButton) {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)
}
So, after the camera launch, how to programmatically handle the Shutter button like clicking it to take photo?

So, after the camera launch, how to programmatically handle the Shutter button like clicking it to take photo?
Tell the UIImagePickerController to takePicture.

Related

Present a popover on button tap in Swift

I would like to make a popover from a button tap. but when I click on my button, my popover cover all my page. where is my problem?
#IBAction func buttonTap(sender: UIButton) {
// get a reference to the view controller for the popover
let popController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "popoverId")
// set the presentation style
popController.modalPresentationStyle = UIModalPresentationStyle.popover
// set up the popover presentation controller
popController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.up
popController.popoverPresentationController?.delegate = self
popController.popoverPresentationController?.sourceView = sender // button
popController.popoverPresentationController?.sourceRect = sender.bounds
// present the popover
self.present(popController, animated: true, completion: nil)
}
I think you need to change the modalPresentaionStyle
popController.modalPresentationStyle = .overCurrentContext

Snapshotting a view that has not been rendered results in an empty snapshot when view presented modally - Swift 3

I have a Table View Controller as a tab. In it, there's a button that takes you to a Facebook profile:
func didTapFacebook() {
let url = URL(string: "http://www.facebook.com/" + myFacebookId)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}
}
Works fine every time you press the button.
From another tab, there's a button that presents modally a navigation controller with Table View Controller as its root:
func segueToTable(_ sender: UIViewController, tvc: MyTableViewController, completion: #escaping ((_ done: Bool) -> Void)) {
let nc = MyNavigationController(rootViewController: tvc)
sender.present(nc, animated: true, completion: {
completion(true)
})
}
…
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tvc = storyboard.instantiateViewController(withIdentifier: "TVC") as! MyTableViewController
segueToTable(self, tvc: tvc, completion: { done in
print(“segue complete”)
})
Now once you tap the same button to go to Facebook profile (or Twitter button, basically anything that causes the app to go to background), this warning occurs (there is no keyboard displayed on the screen at this time):
Cannot snapshot view (>) with afterScreenUpdates:NO, because the view is not in a window. Use afterScreenUpdates:YES.
Returning to the app and pressing the button again causes this warning to appear (and every attempt after that):
Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.
I realize this is related to snapshotting a view that automatically occurs prior to entering the background. It appears for some reason it doesn't like doing that within a view that was presented modally. I've tried a variety of things based on other posts but cannot get the warnings to go away.
Any help is appreciated.

Can't push from programmatically created UINavigationController

In a Swift 3 project using Xcode 8.3 and iOS 10.3, I get a navigation controller to push. The app runs with a navigation controller until I try to use it to push. All works until the last line which cause an app crash, fatal error: unexpectedly found nil while unwrapping an Optional value. I don't want to use the storyboard, the point of this question is how to accomplish this task without the storyboard.
App Delegate code
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: MainViewController())
}
View Controller:
import UIKit
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
let navController = navigationController
let myProfileViewController = MyProfileViewController()
navController!.pushViewController(myProfileViewController, animated: true)
}
}
Move
let navController = navigationController
let myProfileViewController = MyProfileViewController()
navController!.pushViewController(myProfileViewController, animated: true)
from viewDidLoad to viewDidAppear.
In viewDidLoad there is no parent navigation controller set up and you should not start animations from there.
You can add navigation controller in storyboard,
in Storyboard click on MainViewController & from editor menu add navigation controller & push via navigation controller as below code.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyProfileViewController") as! MyProfileViewController
self.navigationController?.pushViewController(vc, animated: true)
Make sure storyboard identifier is correct. & remove navigation controller code form AppDelegate class.
first give name to your navigation controller like this
then in your code initialise it like this
let nav = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourNavigation") as! UINavigationController);
nav.pushFrontViewController(MainViewController(), animated: true);
window?.rootViewController = nav

View Controller perform twice using Segue in Swift 3.0

I'm having a problem in Swift 3.0 right now.
As attached picture (a), I embedded navigationController into ViewController.swift. ViewController.swift have button to connect to UITableCell MovementStatusVC.swift. And MovementDetailsVC.swift is to display all value from table cell.
The problem that i'm facing right now is when I create button in MovementDetailsVC.swift and perform segue to pass the data inside "EditData" function, Page display twice.
MovementDetailsVC.swift
import UIKit
class MovementDetailsVC: UIViewController {
#IBAction func EditData(_ sender: Any) {
performSegue(withIdentifier: "EditMovementDetails", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
The EditMovementDetailsVC.swift controller display twice when I tap Edit button in MovementDetailsVC.swift as shown as in picture below.
I'm using perform segue and prepare segue in MovementDetailsVC.swift
How to fix it so the view only perform one time ?
Thanks.

Automatic presenting View Controller

I have 2 view controllers on my storyboard: viewController1(it input point) and viewController2. After loading the application i want to automatic presenting viewController2 after viewController1.
How i can do that?
if you want to go to second viewcontroller you can add this code to you viewDidLoad method:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "yourIdentifier") as! secondViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
remeber to add a identifier for the second viewcontroller in storyboard and chenge the identifier that you use with "yourIdentifier"
if you don't want a animation put the animated: true to false.
if you dont want to show the fist viewcontroller, go into you storyboard and click on the second viewcontroller, then in the right side on attributes inspector select the box int the image:
For change viewcontroller you have to embed the first viewcontroller with a navigation controller, if you don't know how to do it, just select the fisrt viewcontroller and do like the image, click in navigation controller
Have you tried to show it in func viewDidLoad() {} like:
override func viewDidLoad() {
// ...
self.present(viewController, animated: true, completion: nil)
}
If you want to straight up load a different view controller you can do this in your app delegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "put your identifier here")
self.window?.rootViewController = vc
return true
}