Adsenseコード

2017-09-27

Storyboardを使わずにコードだけで画面を生成、遷移をする

■Storyboardを削除します

■Main Interfaceの変更

左側のプロジェクトディレクトリを選択すると、プロジェクトのプロパティが表示されます その中の General > Deployment Info > Main Interface の部分を空欄にしておきます

■ソースコード

下記のように編集します ```swift: AppDelegate.swift import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = MainTabBarViewController() window?.makeKeyAndVisible() return true } func applicationWillResignActive(_ application: UIApplication) { } func applicationDidEnterBackground(_ application: UIApplication) { } func applicationWillEnterForeground(_ application: UIApplication) { } func applicationDidBecomeActive(_ application: UIApplication) { } func applicationWillTerminate(_ application: UIApplication) { } } ``` ```Swift: Nav1ViewController.swift // 初期表示されるpushボタンがあるページ import UIKit import SnapKit final class Nav1ViewController: UIViewController { private lazy var container: UIView = { let container = UIView() container.backgroundColor = UIColor.gray let button = UIButton(type: .system) container.addSubview(button) button.setTitle("push", for: .normal) button.tintColor = UIColor.white button.backgroundColor = UIColor.blue button.addTarget(self, action: #selector(onTappedPush(_:)), for: .touchUpInside) button.snp.makeConstraints { make in make.width.equalTo(200) make.height.equalTo(40) make.center.equalTo(container) } return container }() override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "Nav1" self.view.addSubview(container) container.snp.makeConstraints { make in make.edges.equalToSuperview() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func onTappedPush(_ sender: UIButton) { print(sender) let vc = SecondViewController(titleName: "second") navigationController?.pushViewController(vc, animated: true) } } ``` ```Swift: SecondViewController.swift // ボタンの遷移先画面 import Foundation import UIKit final class SecondViewController: UIViewController { let titleName: String init(titleName: String) { self.titleName = titleName super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } private lazy var container: UIView = { let container = UIView() container.backgroundColor = UIColor.yellow return container }() override func viewDidLoad() { super.viewDidLoad() navigationItem.title = titleName self.view.addSubview(container) container.snp.makeConstraints { make in make.edges.equalToSuperview() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } ``` ```Swift: Nav2ViewController.swift // 2つ目のタブの画面 import Foundation import UIKit final class Nav2ViewController: UIViewController { private lazy var tableView: UITableView = { let tableView = UITableView() tableView.delegate = self tableView.dataSource = self return tableView }() override func viewDidLoad() { super.viewDidLoad() navigationItem.title = "Nav2" view = tableView } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } extension Nav2ViewController: UITableViewDelegate { } extension Nav2ViewController: UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { return UITableViewCell() } public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 10 } } ``` ```Swift: MainTabBarViewController.swift // AppDelegate.swift で起動時に開くように指定されている下部のタブ部分 import Foundation import UIKit final class MainTabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() let vc = Nav1ViewController() // コントローラのインスタンス vc.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1) // bookmarksという名前でタブとして組み込む let nv = UINavigationController(rootViewController: vc) let vc2 = Nav2ViewController() vc2.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 2) let nv2 = UINavigationController(rootViewController: vc2) setViewControllers([nv, nv2], animated: false) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } ```

※ SnapKit

このコードではSnapKitを使っているので、 SnapKitの導入が必要になります この記事を参考にしてください http://shige-mon.blogspot.jp/2017/09/cocoapodssnapkit.html

参考

http://qiita.com/star__hoshi/items/b38cf99457e781ed7625

0 件のコメント:

コメントを投稿