I'm working with storyboards for my views in a Swift app. I just created a new view controller and made it a class of StreamViewController. For some reason, no content is showing on the storyboard when I simulate the app? The only thing I've added in the storyboard is a Navigation bar.
StreamViewController.swift
import UIKit
class StreamViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
Storyboard Settings
Related
Is there a way to reposition the UIKit tab bar vertically? Like the
gmail app navigation
This question has been asked quite a few times. None of the solutions I found seem to work for me. I’m looking for a solution from scratch in Swift, no third party libs.
Just like what #rmaddy has commented, you will need to create a customised tab and place it in a view as shown below.
FYI, you can easily build a customised tab bar by using buttons.
Once clicked, you just need to add the new controller as a subview by using the extension below.
extension UIViewController {
func add(parentViewController: UIView, childViewController: UIViewController) {
// Add Child View Controller
addChild(childViewController)
// Add Child View as Subview
parentViewController.addSubview(childViewController.view)
// Configure Child View
childViewController.view.frame = parentViewController.bounds
// Notify Child View Controller
childViewController.didMove(toParent: self)
}
}
So from your main view controller, it should look something like this.
class MainController: UIViewController {
// Outlets
#IBOutlet weak var mainView: UIView!
// Lazy loaded controller
private lazy var tab1Controller: Tab1Controller = {
// Instantiate View Controller
let tab1Controller = UIStoryboard(name: "Tab1Controller", bundle: nil).instantiateViewController(withIdentifier: "Tab1") as! Tab1Controller
return tab1Controller
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// MARK: - Tab Bar
#IBAction func Tab1TouchUpInside(_ sender: Any) {
// Navigate to child view
self.add(parentViewController: self.mainView, childViewController: self.tab1Controller)
}
}
Hope it helps!
I need a settings screen in my app which I intend to create with a simple static table view. Unfortunately I ran into an issue. When I assign a custom view controller to the view in my storyboard the views table goes black during runtime.
This is despite the view controller not changing the default behaviour. I only override one method at this point, to put the Settings string in the title.
import Foundation
import UIKit
class SettingsViewController: UITableViewController {
override func loadView() {
self.title = "Settings"
}
}
When I remove the custom view controller the table appears normally.
I segue into the view using the following code.
guard let vc = UIStoryboard(name:"Main", bundle:nil).instantiateViewController(withIdentifier: "settingsViewController") as? UITableViewController else { // or SettingsViewcontroller for the first version
print("Could not instantiate view controller with identifier of type SettingsViewController")
return
}
self.navigationController?.pushViewController(vc, animated:true)
What could cause this issue?
You have to call super.loadView(), You are overriding the UITableViewController's loadView() and not calling the super class method, Due to that you are getting black screen.
import Foundation
import UIKit
class SettingsViewController: UITableViewController {
override func loadView() {
self.title = "Settings"
super.loadView()
}
}
After some googling I found that messing with the default Xcode launch screen is not the most proper way for make ur splash screen wait for some time and other stuff so I add new view controller (custom class named splash) to my storyboard and then after 2 seconds it's will display my main UINavigationController and it's not working just freeze on the splash screen
Here is my code:
import UIKit
class splash: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSThread.sleepForTimeInterval(2.0)
let vc = storyboard?.instantiateViewControllerWithIdentifier("mainmenu") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)
}
}
i have solve it using perform selector
class splash: UIViewController {
override func viewDidLoad() {
super . viewDidLoad()
performSelector(#selector(splash.showmainmenu), withObject: nil, afterDelay: 2)
}
func showmainmenu(){
performSegueWithIdentifier("mainmenu", sender: self)
}
What are you trying to do - is an ugly hack. Don't.
You should create your custom Splash View Controller with layout that mimics your default splash screen image, perform custom animations if any, and then push/present next view controller (on the main thread), or whatever you want to do according to your app requirements.
I have created a subclass of UIViewController called LoginController. I have a LoginController.xib file that contains a view controller with a few elements in it. I have set the class of the view controller to LoginController and I have set my Main Interface to LoginController. Upon launching my app, I see my splash screen, followed by a pure black screen. My LoginController class just has the default code like so
class LoginController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
}
I just created a new xcode project. My results are totally normal. So I guess it's because you are not setting the LoginController.xib as the initial xib to load?
I started an app with a single view controller. I've since added a tab controller view and created 4 views that are setup as tabs, and that all seems ok.
The only viewcontroller.swift file I have is the original one. I'm not sure how to access the view controllers for each of the individual tabs.
Should I just use the one viewcontroller.swift for all my code and link the controls in each tab back to it?
To access the other view controllers, create classes of your UIViewController, like so. I'd recommend you put each class in a separate Swift file in your project, but it's not necessary.
class SecondViewController: UIViewController {
...
}
class ThirdViewController: UIViewController {
...
}
class FourthViewController: UIViewController {
...
}
Then assign them in the Identity Inspector by clicking each Storyboard UIViewController:
I make tabViewController just like this. Make my own viewcontrollers. First, init my own viewcontrollers. Then, push them into tabViewController.viewControllers
class TabbarController: UITabBarController {
override func viewDidLoad(){
super.viewDidLoad();
let first = YourFirstViewController();
let second = YourSecondViewController();
let thrid = YourThirdViewController();
self.viewControllers = [first, second, third];
}
}