Barest minimum code to present a UIView - ios

What's the absolute bare minimum code to create a project that creates a UIView and shows it on the screen of an iOS device?
Without Storyboards, templates or any other contrivance. Just the barest presentation of a UIView.
I appreciate the templates are there to make things "easier", but for the sake of comprehending how everything works together, I'd like to try to conceive the frameworks and their relationships with the OS in the barest, truly code based form.
i.e. without Storyboards and all their processes. Just purely in code.
I've tried using google to find something with someone discussing iOS in an holistic manner, so as to view Views with clarity... but it's all about "ease" rather than understanding, so far as I can see.
With Swift.
// All puns were non-intential byproducts of irritation.
EDIT:::
How do I even start a new Project without a View Controller, Storyboard etc? There seems to be only storyboard based templates, and no way to start a "blank" project.

barest minimum would be this, just place this in your app delegate, delete the other .swift files, and this should be the barest you need:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
rootViewController = UINavigationController(rootViewController: ViewController())
self.window!.rootViewController = rootViewController
self.window!.makeKeyAndVisible()
return true
}
}
this requires Zero storyboards, BUT if you want add a VIEW to all of this and have control over that uiview, then do this:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var firstVC = UIViewController()
var firstVCView = UIView()
firstVCView.backgroundColor = UIColor.redColor()
firstVC.view = firstVCView
rootViewController = UINavigationController(rootViewController: firstVC) self.window!.rootViewController = rootViewController
self.window!.makeKeyAndVisible()
return true
}
}
i can show you from the start, ill start a brand new project and strip it:
here's how:
select sinlge view application project
go into info.plist file, delete these two entries:
Then, delete the storyboard, and the nib.
Keep the "ViewController.swift" file
place this in your AppDelegate.swift:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var rootViewController: UINavigationController?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
rootViewController = UINavigationController(rootViewController: ViewController())
self.window!.rootViewController = rootViewController
self.window!.makeKeyAndVisible()
return true
}
}
then, do this, click no the button "use asset catalog"
the click on the pop up box that you want to use an asset catalog, then make sure this field is blank, where it says "launch screen file"
compile and run your project

A completely empty application with an app delegate, in didFinishLaunchingWithOptions this will present a view:
self.window.rootViewController = [UIViewController new];
there is a view in UIViewController

Related

How to start a new project using AppDelegate and UIKit in Xcode 14.2 [duplicate]

This question already has answers here:
How to remove Scene Delegate from iOS Application?
(6 answers)
Closed last month.
Edit
I mistakenly selected "Multiplatform" instead of "iOS" when creating the project.
Simply select "iOS" to create the project and things will be done the traditional way.
I want to start a new project in Xcode 14.2 with just AppDelegate and UIKit, without SwiftUI or SceneDelegate.
The method up to Xcode 13 does not seem to work.
1. Create new project in Xcode 14.2
2. Add AppDelegate.swift
import UIKit
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = MyViewController()
window!.makeKeyAndVisible()
return true
}
}
3. Add MyViewController.swift
import UIKit
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGreen
print("viewDidLoad() called.")
print("frame:", view!.frame)
}
}
When the application is launched, a green screen is expected to appear, but a black screen is displayed.
Print statements are output correctly.
At this time, the following warning appears.
Adding UIApplicationSceneManifest key to Info.plist will remove this warning, but the black screen will remain. (Previously, this key was not needed for apps that did not use multiple windows.)
[SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
What simple something am I missing?
I assume you want to invoke your app by initializing a UIViewController and don't care much about the underlying UIResponder objects.
Create a new project by selecting Storyboard as the desired Interface preference (You won't use a storyboard after all).
Then add your first View Controller to the window object. And if you'd like to access your AppDelegate you can do it from anywhere as below.
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let window = window else { return }
let vc = UIViewController()
vc.view.backgroundColor = .systemGreen
window.rootViewController = vc
window.makeKeyAndVisible()
let appDelegate = UIApplication.shared.delegate as? AppDelegate
}
}
Correct me if your intention of omitting UIWindowSceneDelegate is of other nature, such as supporting iOS 12

How to set a UITabBarController as rootViewController from Storyboard

Short question:
How can I launch and make a UITabBarController be the rootViewController of my app after starting with a Storyboard?
Long question:
I'm not a swift expert, but I managed to create a complete app using XIBs from the beginning. Now I need my app to start with a Storyboard as a new requirement to post updates to the appstore from 01/07/2020, but I never used it to build my views. It was easy to modify my app to have my Storyboard as an entry point, but the problem is that my initial view today is a TabController, and I don't know how to navigate from my initial Storyboard to my TabController.
My AppDelegate today works something like this:
var window: UIWindow?
func application(_application: UIApplication, didFinishLauchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
// initiate UINavigationControllers and UITabBarController here...
tabController.viewController = [nav1, nav2, nav3, nav4]
tabController.modalPresentationStyle = .fullScreen
self.window!.rootViewController = tabController
self.window!.makeKeyAndVisible()
}
All my attempts ended with a white screen after showing my Storyboard without showing my TabBar.
One of these attempts was this:
override func loadView() {
super.loadView()
// initiate tabController the same way I did in the AppDelegate
UIApplication.shared.keyWindow?.rootViewController = tabController
}
Check the value "Is initial View Controller" for it.

Programmatically set the initial view controller using Storyboards in Xcode 11.2 [duplicate]

This question already has an answer here:
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
(1 answer)
Closed 3 years ago.
I am trying to programmatically set the initial View controller but i keep getting this Error. Any solutions?
2019-11-07 11:47:43.975990+0000 RestaurantApp[16319:147412] [WindowScene] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Here is the code that i have Written.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
let window = UIWindow()
let locationService = LocationService()
let storyboard = UIStoryboard(name: "Main", bundle: nil) //refernce to our storyboard
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//setiing the root view control on our window
switch locationService.status {
case .notDetermined, .denied, .restricted:
let LocationViewController =
storyboard.instantiateViewController (withIdentifier: "LocationViewController") as? LocationViewController
LocationViewController?.locationService = locationService
window.rootViewController = LocationViewController
default:
assertionFailure()
}
window.makeKeyAndVisible()
return true
}
}
Here is an Image of my storyboard
iOS 13 has moved the windows setup from AppDelegate to SceneDelegate to support the use of (possibly multiple) scenes rather than a single window. You now have to do the setup like this:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
let vc = storyboard.instantiateViewController (withIdentifier: "Primary") as! ViewController
window = UIWindow(windowScene: windowScene)
window?.rootViewController = vc
window?.makeKeyAndVisible()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let homeView = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.window?.rootViewController = homeView
return true
}
this works for me
This error happens due to a simple mistake in your storyboard, and it’s easy to fix. When your app starts, iOS needs to know precisely which view controller needs to be shown first – known as your default view controller.
If you accidentally deleted that view controller, or otherwise made it not the default, then you’ll see the error “Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?” when your app launches, along with a plain black screen.
To fix the problem, open your Main.storyboard file and find whichever view controller you want to be shown when your app first runs. When it’s selected, go to the attributes inspector and check the box marked “Is Initial View Controller”. You should see a right-facing arrow appear to the left of that view controller, showing that it’s your storyboard’s entry point.
Go back to storyboard and checkmark this to make the viewController you want your app to start off to,

iOS App is creating two UIWindow

I was refactoring an existing project to move away from using .storyboards and removed the initial main interface in .plist, but for some reason the app is creating two instances of UIWindow.
I have no idea on why this is happening, and the result of this is when I do create my actual UIWindow and use makeKeyAndVisible() for a second I get a black screen until the actual rootViewController becomes visible, this happens because in that split of a seconds it shows the first UIWindow which color is nil. If someone has any idea on why this is happening I would appreciate a bunch ;)
EDIT 1:
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
appCoordinator.start()
return true
}
AppCoordinator.swift
func start() {
let mainVC = UIStoryboard(storyboard: .main).instantiateInitialViewController()
window.backgroundColor = .white
window.rootViewController = mainVC
window.makeKeyAndVisible()
}
Check that "Main Interface" is empty:

Instantiate UINavigationController in AppDelegate

I'm trying to instantiate a UINavigationController with a rootViewController in my AppDelegate. I've looked on this website, but all the examples were from Objective-C or used storyboards (which I'm trying to get away from). 'HomeScreenController` is the root view of the application.
Edit: this shows up in the console
2015-07-18 14:42:25.376 FastFactsSwift[4749:343495] Failed to instantiate the default view controller for UIMainStoryboardFile 'Main'
- perhaps the designated entry point is not set?
How do I fix this?
The following code results in just a black screen:
AppDelegate.swift:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window?.rootViewController = UINavigationController(rootViewController: HomeScreenController())
return true
}
...
HomeScreenController.swift:
import UIKit
class HomeScreenController: UIViewController, UISearchBarDelegate, UITableViewDelegate{
override func viewDidLoad(){
super.viewDidLoad()
var width = self.view.viewWidth
var height = self.view.viewHeight
//Add stuff to the view
}
...
Why is HomeScreenController showing up as a black screen?
The problem was not configuring AppDelegate to not use a storyboard.
How do I create a new Swift project without using Storyboards?

Resources