Swift 项目中 TabBar 控制器实战演示

这份示例代码演示了如何在 Swift iOS 项目中使用 TabBar 控制器。TabBar 控制器在 iOS 应用里很常见,它可以让用户通过底部的标签栏在不同的视图之间切换,使用起来很方便。

想用好 TabBar 控制器,你需要知道以下几点:

  1. 创建 TabBar 控制器: 你可以通过故事板或代码来创建 TabBar 控制器。在故事板里,你可以直接把 TabBar 控制器拖到界面上,然后设置它的子视图控制器;在代码里,你可以使用 UITabBarController 类来创建一个新的实例。

  2. 配置子视图控制器: TabBar 控制器通常包含多个子视图控制器,每个子视图控制器对应一个标签。这些子视图控制器可以是导航控制器包装的其他控制器,这样就能提供额外的导航功能。在 Swift 中,你可以通过 viewControllers 属性来添加和设置子视图控制器。

let vc1 = UIViewController()
let vc2 = UIViewController()
tabBarController.viewControllers = [vc1, vc2]
  1. 自定义 TabBar 项目: TabBar 项目指的是显示在 TabBar 上的图标和文字。你可以使用 tabBarItem 属性来自定义每个子视图控制器的 TabBar 项目。比如,你可以改变标题和图片:
vc1.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_selected"))
vc2.tabBarItem = ...
zip
92a80f4170b11ba5b822eda7a1bdddf7.zip 预估大小:43个文件
folder
demo6-TabbarVc的使用 文件夹
file
.DS_Store 6KB
folder
demo6-TabbarVc的使用.xcodeproj 文件夹
folder
project.xcworkspace 文件夹
file
contents.xcworkspacedata 183B
folder
xcuserdata 文件夹
folder
zero.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 25KB
folder
xcuserdata 文件夹
folder
zero.xcuserdatad 文件夹
folder
xcdebugger 文件夹
file
Breakpoints_v2.xcbkptlist 91B
folder
xcschemes 文件夹
file
xcschememanagement.plist 495B
file
demo6-TabbarVc的使用.xcscheme 4KB
file
project.pbxproj 25KB
folder
demo6-TabbarVc的使用 文件夹
file
DHTabbarButton.swift 2KB
file
second@3x.png 2KB
file
second_select@3x.png 2KB
folder
ImageHelper 文件夹
file
.DS_Store 6KB
file
ImageHelper.swift 30KB
file
ImageVIewExtension.swift 1KB
file
ViewController.swift 522B
file
SecondViewController.swift 940B
file
CustomTabbarController.swift 2KB
file
Info.plist 1KB
file
AppDelegate.swift 2KB
file
2.jpg 26KB
file
DHScrollView.swift 9KB
file
first@2x.png 447B
file
DHNavigationController.swift 2KB
file
1.jpg 29KB
file
first_select@2x.png 436B
file
DHRootBaseViewController.swift 1KB
file
four_select@3x.png 1012B
file
SecondViewController.xib 2KB
file
4.jpg 19KB
file
first_select@3x.png 610B
folder
Base.lproj 文件夹
file
LaunchScreen.storyboard 2KB
file
Main.storyboard 2KB
file
Then.swift 1KB
file
first@3x.png 598B
file
FourViewController.swift 889B
file
ThirdViewController.swift 2KB
file
third@3x.png 2KB
file
DHTabbarViewController.swift 2KB
folder
Assets.xcassets 文件夹
folder
AppIcon.appiconset 文件夹
file
Contents.json 585B
file
DHTabBar.swift 2KB
file
third_select@3x.png 2KB
file
four@3x.png 1004B
file
FirstViewController.swift 3KB
file
3.jpg 39KB
zip 文件大小:192.78KB