Author: Sean Allen Video URL: Intro to Programatic UI. No Storyboard – Swift – Xcode 10 Introduction: In this video we build the basic structure of an app without using storyboards. We take a programatic approach to showing the initial screen, creating a UINavigationController, adding a UIButton to the screen, and transitioning to a new screen. This is just the beginning o your programatic UI journey!
Could not find a storyboard named ‘Main’ in bundle NSBundle
When CMD+R to run the project, will get the error of “Could not find a storyboard named ‘Main’ in bundle NSBundle” after deleting the default MainStoryboard file. The solution is to delete the propriety of “Main storyboard file base name” in the file of Info.plist.
Add initial view controller
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let vc = ViewController() window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = vc window?.makeKeyAndVisible() return true }
Set Constraints by codes
func setNextButtonConstraints() {
nextButton.translatesAutoresizingMaskIntoConstraints = false
nextButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20).isActive = true
nextButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20).isActive = true
nextButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
nextButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
These four constraints will restrict the explicit position of nextButton in its super view i.e. view.
Add target for the button
/* This will add the specific target of "nextButtonTapped" to nextButton. */
nextButton.addTarget(self, action: #selector(nextButtonTapped), for: .touchUpInside)
/* The target function looks like as follows. */
@objc func nextButtonTapped() {
/* TODO */
}
分类:iOS