Programmatically Access XIB Launch Screen or Storyboard

How can an application use the XIB or storyboard used to launch the screen? XIB is not in the main bundle (for example: NSBundle.mainBundle().pathsForResourcesOfType(nil, inDirectory: "") ). This is especially unexpected, since "Launch Screen.xib" is specified in the "Copy Stock" build phase, but does not show ip in the package, so Xcode must handle it specifically.

+6
source share
3 answers

Since Xib is not part of the main bundle, the get path returns nil, but you can get the XIB of the startup screen without using the path using the method

  let launchScreenNib = UINib(nibName: "LaunchScreen", bundle: nil) 

or

You can load views from XIB as

 // Swift let objects = NSBundle.mainBundle().loadNibNamed("LaunchScreen", owner: self, options: nil) let view = objects[0] as UIView // Obj C NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"LaunchScreen" owner:self options:nil]; UIView *view = [objects objectAtIndex:0]; 
+5
source

If the LaunchScreen storyboard is not xib , use the following code.

 let launchScreen = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController() if let launchView = launchScreen?.view { view.addSubview(launchView) } 
+4
source

According to my own question, it turned out that turning off and starting โ€œUse as Screen Launcherโ€ caused Xcode to create a NIB file for the launch screen.

  • After completely uninstalling (removing DerivedData) and uninstalling the application from the device, I disabled โ€œUse as start screenโ€ for the main view controller in LaunchScreen.xib. Launching the application started launching without a launch screen, but now the assembly has created LaunchScreen.nib.
  • I cleaned and uninstalled the application from the device again. Switch "Use as start screen" and rebuild again. The bundle of applications in the new DerivedData folder LaunchScreen.nib was still there.
  • bundle.loadNibNamed(...) now works fine.

2G3l3.png

0
source

Source: https://habr.com/ru/post/975623/


All Articles