I am creating an app with Swift 4.0 and I am trying to delay the launch screen. The initial screen will sleep for 2 seconds, then the "a FusionPointInc application" label will disappear and the screen will stay at the red curtain background image (the second image). It will stay there and won't show the second screen (the Theater Stages Throughout History screen)
Here is my code for the initial screen:
override func viewDidLoad() {
super.viewDidLoad()
sleep(2)
showNavController()
}
func showNavController() {
performSegue(withIdentifier: "showMainView", sender: self)
}
More code for the initial screen (will transition to the second screen):
`class showMainView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//performSegue(withIdentifier: "showSegue", sender: self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
This screen will open and when prompted to segue, the screen will only show the background (the second picture).
The middle screen code is in the ViewController.swift file and it has no actual code in it (just the pre-formatted base code)
I tested when I delete the initial screen, the app will load up fine. But, I wanted the initial screen to be delayed so "a FusionPointInc application" can be seen!
You are calling sleep on the main thread.
Instead, you should be doing something like this:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) {
self.showNavController()
}
}
This will call showNavController after two seconds without issues.
Related
In my application I used SWRevealViewController in order to implement the side menu. There, for some reason I had to put a view which is embed in a navigation controller, before the reveal view controller.
This is my storyboard
Everything works fine other than one thing. when I drag from the left edge of the screen to the right side (pan gesture) on my home view, instead of side menu it navigates me to the previous view (in here case to the middle view which contains a button in the middle).
This is how it looks like when dragging
I want to avoid this and get the side menu when dragging like that. Can someone help me on this. Any help would be highly appreciated.
Edit:
This is the front_view and rear_view
Take a ViewController.swift file for the initial ViewController having button.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
There is no issue in the swrevealviewcontroller implementation but the native behavior of navigation controller is causing this issue.
You need to remove the popGesture from the navigation controller in any view controllers that you don't want to be able to use the swipe gesture:
var gestureRecognizer: UIGestureRecognizer? {
guard let nc = navigationController else { return nil }
return nc.interactivePopGestureRecognizer
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let gr = gestureRecognizer {
gr.isEnabled = false
}
}
I am trying to move a picture in Xcode 8. I have the code for it, but the image won't move when the button is pressed. Can someone please help me solve the problem for the picture not moving.
import UIKit
class Level2ViewController: UIViewController {
var image = UIImage(named: "ballon.png")
var imageView = UIImageView(image: "ballon")//initialize properly, this is just for reference
#IBAction func buttonPressed(_ sender: UIButton) {
self.imageView.frame.origin.x += 50
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
Delete your button from your code and then right click on your button and click on the "x" to remove the connection to the old code.
Now add the button back. I think your button was setup wrong.
when you right click the botton look and see what the Send Event was.
It should be setup this way.
If this is the answer you were looking for don't forget to except the answer.
Ok I think I see it.. You need to control drag your imageView to your code and connect it as an outlet and then move the imageView outlet. you are moving the variable.
If this is the answer you were looking for don't forget to except the answer.
I am trying to do a segue to another screen using code but it shows me a black screen using Xcode 7 beta 6.
here is my 1st view controller file code
// ViewController.swift
// Segue through programming
//
import UIKit
class ViewController: UIViewController{
#IBAction func buttonPressed(sender: AnyObject) {
presentViewController(secondController(), animated: true) { () -> Void in
}
}
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.
}
}
This will only work if secondController programmatically creates its view. If you want to use a storyboard scene (which is far more common), you can do the following:
#IBAction func buttonPressed(sender: AnyObject) {
let controller = storyboard?.instantiateViewControllerWithIdentifier("foo")
presentViewController(controller!, animated: true, completion: nil)
}
That obviously assumes that you've specified a storyboard identifier for the destination scene.
Or, you can created a segue between the two scenes in IB by control dragging from the view controller icon at the top of the first scene to the second scene:
and then give that segue its own storyboard id:
Then you can invoke the segue programmatically:
#IBAction func buttonPressed(sender: AnyObject) {
performSegueWithIdentifier("bar", sender: self)
}
I have a split view controller. Everything loads properly, except when you're in portrait and you rotate to landscape, the cell current gets de-selected.
I found the problem. The table was reloading data every time the viewWillAppear function was called, and viewWillAppear is called every time the device is rotated.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
table.reloadData()
}
Now there's a new issue. I need to update the TableView whenever I add an item from a modal view.
The modal view is another view controller. I tried:
MasterViewContoller().table.reloadData()
That raised a bunch of flags and I'm pretty sure that's not the right way to do it. So how can I reload the table from another view?
==============
For those think that the ViewWillAppear is not called on rotation, try this and see:
class MasterViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
println("rotated")
}
}
class DetailedViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
The standard solution to your problem is to use a delegate.
protocol ModalViewControllerDelegate {
func updateInModalViewController(sender: ModalViewController)
}
class MainViewController: UIViewController, ModalViewControllerDelegate {
func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
if let controller = segue.destinationViewController as? ModalViewController {
controller.delegate = self
}
}
// the rest should be pretty obvious
}
import UIKit
class CollectionHolderViewController: MainPageContentViewController { //MainPageContentViewController inherits from UIViewController
var collectionViewController = UICollectionViewController()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionViewController = self.storyboard!.instantiateViewControllerWithIdentifier("TrophyRoom") as UICollectionViewController
// Do any additional setup after loading the view.
self.addChildViewController(collectionViewController)
self.view.addSubview(collectionViewController.view) //I tried collectionViewController.collectionView with no success either
self.collectionViewController.didMoveToParentViewController(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
This code worked for a UIPageViewController but not for this UICollectionViewController.
I just see a blank white (default) UIViewController which is expected if the UICollectionViewController's view was NOT set. There is no error shown in XCode 6.
Help?
Edit: The code DOES in fact work : ) Just a tip, set the collectionViewController.view to have a frame that fills the screen for consistency otherwise it will fit just below the status bar.