Programmatically creating UINavigationController in iOS
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ImageViewController2 *dealVC = (ImageViewController2 *)[storyboard instantiateViewControllerWithIdentifier:@"ImageViewController2"];
[self.navigationController pushViewController:dealVC animated:YES];
where ImageViewController2 is a class name
Creating a navigationController programmatically (Swift)
In AppDelegate.swift
Swift 1, 2:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var nav1 = UINavigationController()
var mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
}
Swift 4+: and Swift 5+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
let mainView = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
}
How to create a UINavigationControll in UIViewController programmatically Swift
Maybe have a look at "Showing and Hiding View Controllers" where some navigation concepts are explained: https://developer.apple.com/documentation/uikit/view_controllers/showing_and_hiding_view_controllers
When using storyboards you should be able to use a segue to navigate from one view controller to another.
Create a UINavigationController programmatically
This looks like a case of unoptimized app for iPhone X and others. How are you doing the launchscreen? Since I don't see a LaunchScreen.storyboard I assume you are using image assets. That is frowned upon because you would need to update your app once a new screen size is created.
TL:DR Use LaunchScreen.storybooard
for Launch Screen File
VS Launch Images Source
EDITED: Create a LaunchScreen.storyboard and set it to Launch Screen file
navigation controller that i create programmatically not appearing as in examples
If you are using Xcode 11 and iOS 13, you need to write same code in SceneDelegate.swift
. The main reason for Apple to add UISceneDelegate
to iOS 13 was to create a good entry point for multi-windowed applications.
If you do not intend to support multiple windows, you can remove SceneDelegate.swift
file remove UIApplicationSceneManifest
key from Info.plist and remove func application(_: configurationForConnecting:, options:)
and func application(_:, didDiscardSceneSessions:)
methods from AppDelegate.swift
Learn more about Scene Delegate on donnywalls - Understanding the iOS 13 Scene Delegate
How to add a navigation controller programmatically in code but not as initial view controller
You can add UINavigationController
like below :
First you have to create object of SecondViewController
,
let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
Or
let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
Or
let myViewController: SecondViewController? = SecondViewController()
Then add Navigation to SecondViewController
let myNavigationController = UINavigationController(rootViewController: myViewController!)
If you want to present then use :
self.present(myNavigationController, animated: true) {
}
If you want to push then use :
self.navigationController?.pushViewController(myNavigationController, animated: true)
If you want to set as root controller then use :
let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = myNavigationController
How to add navigation controller programmatically?
try as below
UIViewController *bbp=[[UIViewController alloc]initWithNibName:@"UIViewController" bundle:nil];
UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:bbp];
// [self.navigationController presentModalViewController:passcodeNavigationController animated:YES];
[self.navigationController pushViewController:passcodeNavigationController animated:YES];
[passcodeNavigationController release];
iOS: Navigation Bar of a Programmatically created UINavigationController Not Expanding to Safe Area
Just add this to the end of your navigationBarConfiguration
func
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.backgroundColor = UIColor.systemBlue
controller.navigationBar.standardAppearance = navBarAppearance
controller.navigationBar.scrollEdgeAppearance = navBarAppearance
} else {
controller.edgesForExtendedLayout = []
}
UINavigationController appDelegate UIViewController programmatically
Result:
Code:
AppDelegate:
window = UIWindow()
let firstVC = FirstViewController()
let navigationController = UINavigationController(rootViewController: firstVC)
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
FirstVC:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
button.backgroundColor = .red
view.addSubview(button)
button.addTarget(self, action: #selector(showSecondVC), for: .touchUpInside)
}
@objc func showSecondVC() {
let secondVC = SecondViewController()
navigationController?.pushViewController(secondVC, animated: true)
}
SecondVC:
view.backgroundColor = .yellow
Hope it helps! Please let me know if it works for you or not.
Related Topics
iOS Facebook Login "Given Url Is Not Allowed by the Application Configuration"
Multiple Localized .Strings Files in iOS App Bundle
Instantiateviewcontrollerwithidentifier - Storyboard Id Set But Still Not Working
Xcode:Why Launchoptions in Didfinishlaunchingwithoptions Always Nil
Swiftui Foreach Index Out of Range Error When Removing Row
Uilabel, Uifont and Utf-8 Triangle
How to Detect When User Used Password Autofill on a Uitextfield
How to Capture Taps Ignored by Wkwebview
Autolayout: Uiview Within Uiview Has Incorrect Frame
Handing Firebase + Facebook Login Process
Avplayer with Playback Controls of Avplayerviewcontroller
Setting a Rotation Point for Cgaffinetransformmakerotation Swift
Swiftui: How to Get Continuous Updates from Slider
Mkannotationview and Tap Detection
Capture Location in All States App