How to add UIView on window in app delegate in swift? - swift

view object is not getting in addsubview bracket
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
{
// Override point for customization after application launch.
UIApplication.sharedApplication().setStatusBarHidden(true, animated: true);
var myView = UIView(frame:CGRectMake(0, 200, 320, 100));
myView.backgroundColor = UIColor.redColor()
self.window.?.addSubview(myView)
return true
}

Try to use so if access through UIViewController
let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
win.addSubview(self)

please, do the implementation for the stack properly, like e.g. this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainViewController: MainViewController = MainViewController(nibName: "MainViewController", bundle: nil)
window!.rootViewController = mainViewController
window!.makeKeyAndVisible()
return true
}

In addition to your code,
for UIApplication Root view controller's viewDidAppear Function
add below code.
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
UIApplication.sharedApplication().delegate!.window!!.bringSubviewToFront((UIApplication.sharedApplication().delegate!.window!!.subviews[0]))
}
Hope that helps :)

Related

I cannot change the background color of collection view programmatically

I do not know why I cannot change the first screen programmatically. The first screen remains white even if I change the color of collection view background color. I need someone's help.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
window?.makeKeyAndVisible()
let layout = UICollectionViewFlowLayout()
let navController = UINavigationController(rootViewController: PokemonController(collectionViewLayout: layout))
window?.rootViewController = navController
return true
}
This is the code inside the Controller file. I deleted the default controller file.
class PokemonController: UICollectionViewController {
//property
//Init
override func viewDidLoad() {
super.viewDidLoad()
configureViewComponent()
}
func configureViewComponent() {
collectionView.backgroundColor = .white
navigationController?.navigationBar.barTintColor = .mainPink()
}
}
Thank you!
Did you try it like this
func configureViewComponent() {
backgroundColor = .white
navigationController?.navigationBar.barTintColor = .mainPink()
}
try this and let me know, if it works.
func configureViewComponent() {
collectionView.backgroundView?.backgroundColor = .red
navigationController?.navigationBar.barTintColor = .mainPink()
}

UINavigationBar not showing

I have a swift project that is programmatic and segues from a mapview to another view. After the segue the navigation bar is not present. Since the files were copied over from an earlier project where this doesn't happen I'm perplexed. It should be very straight forward.
In AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let homeViewController = MapViewController()
self.navigationController = UINavigationController()
self.navigationController?.setNavigationBarHidden(false, animated: false)
navigationController?.viewControllers = [homeViewController]
self.window!.rootViewController = navigationController
self.window?.makeKeyAndVisible()
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
return true
}
And the segue code called in the MapViewController:
let storeViewController = ViewController()
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.navigationController?.setNavigationBarHidden(false, animated: false)
appDelegate.navigationController?.pushViewController(storeViewController, animated: true)
You have to embedded MapViewController in UINavigationController and Push ViewControllers as needed
to hide use self.navigationController?.isNavigationBarHidden = false
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
let homeViewController = MapViewController()
let navController = UINavigationController(rootViewController: homeViewController)
self.window!.rootViewController = navController
self.window?.makeKeyAndVisible()
let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes
return true
}
and in MapViewController
To navigate:
let storeViewController = ViewController()
self.navigationController?.pushViewController(storeViewController, animated: true)
//if you want to hide or show navigation
//self.navigationController?.isNavigationBarHidden = false // true

Self.revealViewController() returns nil

I am currently trying to get the pan gesture to reveal the controller. As of right now, my open button works, but when I add the last line, my program crashes because revealViewController is nil. Link to pod: https://github.com/John-Lluch/SWRevealViewController.
import Foundation
import UIKit
class MainViewController : UIViewController{
#IBOutlet weak var mainButton: UIView!
#IBOutlet weak var Open: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated);
mainButton.makeCircle();
Open.target = self.revealViewController()
Open.action = #selector(SWRevealViewController.revealToggle(_:));
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer());
}
}
It's likely you haven't set your SWRevealViewController to be the rootViewController. Remember to set Custom Class and Storyboard ID to be SWRevealViewController in your Identity inspector.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
self.view.window?.rootViewController = rootVC
// Cont. with your code...
}
In case you need to show a login page at the beginning, you need a function in you AppDelegate, which allows the window to update the rootViewController
func changeRootVCToSWRevealVC () {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: "SWRevealViewController")
if let window = self.window{
window.rootViewController = rootVC
}
}
After user logged in, you can change the rooViewController to SWRevealViewController.
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.changeRootVCToSWRevealVC()
}
If you have a variable storing the state of the last log-in, and want to navigate directly to the Navigation Drawer menu rather than LoginViewController.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let rootVCId = isLoggedin ? "SWRevealViewController" : "LoginViewController"
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewController(withIdentifier: rootVCId)
self.view.window?.rootViewController = rootVC
// Cont. with your code...
}
I was just having this same issue and came across this post. My issue turned out to be more of an "oops!" as I missed something obvious and spent 30 minutes scratching my head on it. :facepalm:
If the accepted answer doesn't work for you, check to make sure you connected your menu property to the storyboard with an outlet.
Swift:
#IBOutlet weak var menuButton:UIBarButtonItem!
override func viewDidLoad() {
if (self.revealViewController() != nil) {
menuButton.target = self.revealViewController()
menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
}
}

How to create a simple UIViewController in Swift and set it as the starting view

I would like to create a simple UIViewController and when I start the simulator it should be the first View that gets called. The content never loads though. For test purposes I only add a label.
Here is my UIViewController:
import Foundation
import UIKit
class ViewControllerSearchGithubName:UIViewController {
let lblName = UILabel(frame: CGRectMake(15, 15, 50, 50))
override func viewDidLoad() {
super.viewDidLoad()
lblName.text = "Name:"
lblName.textColor = UIColor.cyanColor()
self.view.addSubview(lblName)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
and my AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let viewController = ViewControllerSearchGithubName()
let navc = UINavigationController(rootViewController: viewController)
self.window?.rootViewController = navc
self.window?.makeKeyAndVisible()
return true
}
I apprecciate any kind of help.
You code is working fine; your view controller and its view are behaving as expected. The problem is only that your label is at 15,15, which is underneath the navigation bar and thus hidden by it.

how to passing coredatastack from AppDelegate to the view controller without navigation controller

I know the way to pass coreDataStack to the viewController with navigation controller embedded by this way.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let navigationController = window!.rootViewController as! UINavigationController
let viewController = navigationController.topViewController as! ViewController
viewController.coreDataStack = coreDataStack
return true
}
I want to pass the coreDataStack to my viewController without the navigation controller,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("Home") as! Home
viewController.coreDataStack = coreDataStack
return true
}
However, the coreDataStack in the viewController found to be nil.
How can I do it?
Your code is creating a new instance of the view controller, not referencing the existing instance. You should be able to use this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let viewController = window!.rootViewController as! ViewController
viewController.coreDataStack = coreDataStack
return true
}