Custom Splash Screen for iOS App - ios

After some googling I found that messing with the default Xcode launch screen is not the most proper way for make ur splash screen wait for some time and other stuff so I add new view controller (custom class named splash) to my storyboard and then after 2 seconds it's will display my main UINavigationController and it's not working just freeze on the splash screen
Here is my code:
import UIKit
class splash: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSThread.sleepForTimeInterval(2.0)
let vc = storyboard?.instantiateViewControllerWithIdentifier("mainmenu") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)
}
}

i have solve it using perform selector
class splash: UIViewController {
override func viewDidLoad() {
super . viewDidLoad()
performSelector(#selector(splash.showmainmenu), withObject: nil, afterDelay: 2)
}
func showmainmenu(){
performSegueWithIdentifier("mainmenu", sender: self)
}

What are you trying to do - is an ugly hack. Don't.
You should create your custom Splash View Controller with layout that mimics your default splash screen image, perform custom animations if any, and then push/present next view controller (on the main thread), or whatever you want to do according to your app requirements.

Related

Stop QLPreviewController from resizing in presented View Controller

I'm presenting a NavigationViewController in iPad app as formSheet, which works great. But in this presented NavigationViewController when I push QLPreviewController with one Image to display it resizes it self to fit the Image. This alone would not have been a problem but upon going back to previous viewController form QLPreviewController the resized viewController size affect all ViewControllers in that NavgationViewController.
If there is any way to go back to default size for viewController on coming back from QLPreviewController it would help too. Or If QLPreviewController doesn't resize when pushed.
Also this problem is only in iOS 13+.
Any help would be appreciated.
Finally, I resolved it on my own.
Create one Variable
var defaultPreferredContentSize: CGSize?
and one method
fileprivate func adjustViewControllerSizeIfNeeded() {
if let size = defaultPreferredContentSize {
if self.navigationController?.preferredContentSize != size {
self.navigationController?.preferredContentSize = size
}
} else {
defaultPreferredContentSize = self.view.frame.size
}
}
Call this method in viewDidAppear
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
adjustViewControllerSizeIfNeeded()
}

Reusing 'expensive' UIViewController

I am embedding an AVPlayerViewController(which is expensive in terms of resources) in a UIViewController (using containment). I need to use another AVPlayerViewController in the subsequently pushed view controller in navigation stack, but it would be nice if I can remove it from the parent & embed it in the child. When the child pops, I want to embed it back in the parent. What is the elegant way to do this (code or storyboards)?
You need to do it in code. Create your 'expensive' view controller and store it using strong reference somewhere. You can show it programmatically anywhere, then you can dismiss it, but it will be store by strong reference. Later you can show it again.
P.S. looks like you view controller have AVPlayer, in this case probably you need to add some method to 'wipe' its state before reuse or at leave pause playback
It can be done like this way. You need to setup player .
import UIKit
import AVKit
class AVViewController: UIViewController {
static var player : AVPlayerViewController?
#IBOutlet var containerView: UIView!
override func viewWillAppear(_ animated: Bool) {
DispatchQueue.main.async {
self.addChildViewController(AVViewController.player!)
self.containerView.addSubview((AVViewController.player?.view)!)
// setup player here.
}
super.viewWillAppear(animated)
}
override func viewDidLoad() {
super.viewDidLoad()
for vc in self.childViewControllers{
if let vc = vc as? AVPlayerViewController, AVViewController.player == nil {
AVViewController.player = vc
}
}
// Do any additional setup after loading the view.
}
}

Swift: Popups and ViewDidAppear etc

I'm a novice Swift-programmer who wants a popup window (in a separate view/view controller) to popup immediately when an app is loaded (even before the main view is displayed if possible).
I use the following code to popup
if let vc = storyboard?.instantiateViewController(withIdentifier: "PasswordPopUp") as? PasswordPopUp {
vc.modalPresentationStyle = .overCurrentContext
present(vc, animated: true, completion: {
//self.cleanP2action()
})
} else {
print("error creating PasswordPopUp")
}
This works fine if I use it by clicking a button for instance. But I would like it to happen automatically. When I add the code to viewDidLoad i get an error msg
Warning: Attempt to present <Mygame.PasswordPopUp: 0x7fbfd2c252f0> on
<Mygame.ViewController: 0x7fbfd3876000> whose view is not in the
window hierarchy!
I tried to stick it in viewDidAppear but this does not seem to be called. Here is the essentials of the main view controller:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
What's wrong and how do I fix it?

Implement PopOver in Swift

I want to show a popup menu and without using nib.I dont like using nib since the headache to implement delegate for a simple functionality. I succeeded using modalPresentationStyle as a Popover to show ViewController as a Popover and it works fine with the below code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var btnShowPopOver: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func showPopUP(sender: AnyObject) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let popVC = storyboard.instantiateViewControllerWithIdentifier("pop")as! PopViewController
popVC.modalPresentationStyle = UIModalPresentationStyle.Popover
popVC.preferredContentSize = CGSizeMake(320, 240)
popVC.popoverPresentationController!.delegate = self
let popOverController = popVC.popoverPresentationController
popOverController!.sourceView = sender as! UIView // where to stick the bar item in which view
popOverController!.sourceRect = CGRectMake(70,30, 0, 0) //where to stick the bar
popOverController?.permittedArrowDirections = nil
self.presentViewController(popVC, animated: true, completion: nil)
}
}
extension ViewController :UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyleForPresentationController(PC: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
func popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
var controller = popoverPresentationController.presentedViewController as! PopViewController
println(" this is data from pop view controller \(controller.textField.text)")
}
}
Apple doc says
Popover controllers are for use exclusively on iPad devices.
Attempting to create one on other devices results in an exception.
I tested this on real device iphone-6 and its working fine..I am loving this Popover.
Should i use Popover or not as per the apple documentation?Since its working fine in iphone,will my app get rejected for using it later?
Since iOS8 we are now able to create popovers, that will be the same
on iPhone, as on iPad, which would be especially awesome for those who
make universal apps, thus no need to make separate views or code.
Source : UIPopoverController for iphone not working?
Look at this answers :
https://stackoverflow.com/a/14789022/3202193
https://stackoverflow.com/a/30418212/3202193
In the per-release document of iOS 9 also they are saying like :
The UIPopoverController class is used to manage the presentation of content in a popover. You use popovers to present information temporarily. The popover content is layered on top of your existing content and the background is dimmed automatically. The popover remains visible until the user taps outside of the popover window or you explicitly dismiss it. Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception.
EDIT
I think you have used the UIPopoverPresentationController which is Available in iOS 8.0 and later.
The UIPopoverController and UIPopoverPresentationController are two different things provided by Apple.

Calling segue programmatically not working

I am trying to make a splash screen. I have a view that has a background image being drawn onto it and then another view I want to transition to after a few seconds. I am trying to use the following code:
self.performSegueWithIdentifier("showApp", sender: self)
I created a segue between the two views by ctrl+dragging a line from one to the other. I set the segue's identifier to "showApp".
When I run the code nothing happens and there are no errors. Any ideas?
Here is the controller for the splash screen:
class SplashViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
sleep(2)
// THIS DOES NOTHING:
self.performSegueWithIdentifier("showApp", sender: self)
// THIS AS SUGGESTED ALSO DOES NOTHING:
var otherViewController:UIViewController = self.storyboard.instantiateViewControllerWithIdentifier("test") as UIViewController
self.presentViewController(otherViewController, animated: true, completion: nil)
}
}
Normally, you need a navigation controller in order to use segue.
Highlight your SplashViewController object inside the Storyboard and go to
Editor -> Embeded In -> Navigation Controller
After that, remove the code suggested by Clement and try running the project again, you should get the result that you expected.

Resources