Skip to content

Implement UITabBarController.

   

This is a sample implementation of UITabBarController, which is used to switch views.
The icon for UITabBarController is a 30×30 transparent png.

alt alt

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var tabBarController: UITabBarController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//tabb
UITabBar.appearance().tintColor = UIColor.orange
UITabBar.appearance().unselectedItemTintColor = UIColor.gray
UITabBar.appearance().barTintColor = .purple
UITabBar.appearance().isTranslucent = false
// ページを格納する配列
var viewControllers: [UIViewController] = []
let firstViewController: FirstViewController? = FirstViewController()
firstViewController?.tabBarItem = UITabBarItem(title: "First", image: UIImage(named: "tab-icon-sample"), tag: 1)
viewControllers.append(firstViewController!)
let secondViewController: SecondViewController? = SecondViewController()
secondViewController?.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.downloads, tag: 2)
viewControllers.append(secondViewController!)
let thirdViewController: ThirdViewController? = ThirdViewController()
thirdViewController?.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarSystemItem.history, tag: 3)
viewControllers.append(thirdViewController!)
tabBarController = UITabBarController()
tabBarController?.setViewControllers(viewControllers, animated: false)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window!.makeKeyAndVisible()
window?.rootViewController = tabBarController
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .green
let firstViewLabel = UILabel()
firstViewLabel.frame = self.view.frame
firstViewLabel.textAlignment = .center
firstViewLabel.font = UIFont.boldSystemFont(ofSize: 40)
firstViewLabel.textColor = .black
firstViewLabel.text = "First View"
self.view.addSubview(firstViewLabel)
}
}
import UIKit
class SecondViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .orange
let firstViewLabel = UILabel()
firstViewLabel.frame = self.view.frame
firstViewLabel.textAlignment = .center
firstViewLabel.font = UIFont.boldSystemFont(ofSize: 40)
firstViewLabel.textColor = .black
firstViewLabel.text = "Second View"
self.view.addSubview(firstViewLabel)
}
}
import UIKit
class ThirdViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
let firstViewLabel = UILabel()
firstViewLabel.frame = self.view.frame
firstViewLabel.textAlignment = .center
firstViewLabel.font = UIFont.boldSystemFont(ofSize: 40)
firstViewLabel.textColor = .black
firstViewLabel.text = "Third View"
self.view.addSubview(firstViewLabel)
}
}

  1. Using Color Set to manage colors in Asset Catalog
  2. I sent a code-level question to an Apple engineer.
  3. Grading the UIView background
  4. Implementing a custom button class
  5. Edit TableView to delete a Cell
  6. Edit TableView and delete a Cell (side-slide).
  7. Separate numbers into commas for each three digits (in Japanese yen)