{"id":392,"date":"2025-06-04T10:36:21","date_gmt":"2025-06-05T03:26:20","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=392"},"modified":"2025-06-05T10:36:21","modified_gmt":"2025-06-05T03:26:20","slug":"swift-tabbarnavigationbar-%e7%af%84%e4%be%8b-%e4%b8%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-tabbarnavigationbar-%e7%af%84%e4%be%8b-%e4%b8%8b\/","title":{"rendered":"2025 \u6700\u65b0 Swift TabBar \u8207 NavigationBar \u5b8c\u6574\u5be6\u4f5c\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h3>\u7c21\u8981<\/h3>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 Swift \u4e2d\u8a2d\u5b9a TabBar \u548c NavigationBar\u3002\u9019\u5c07\u5e6b\u52a9\u4f60\u5efa\u7acb\u4e00\u500b\u529f\u80fd\u9f4a\u5168\u7684\u61c9\u7528\u7a0b\u5f0f\u5e95\u5c64\u67b6\u69cb\uff0c\u4e26\u78ba\u4fdd\u4f60\u7684 UI \u8a2d\u8a08\u7b26\u5408\u73fe\u4ee3\u61c9\u7528\u7684\u9700\u6c42\u3002<\/p>\n<h3>UINavigationController<\/h3>\n<p>\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u5728 `viewDidLoad` \u65b9\u6cd5\u4e2d\u9032\u884c NavigationBar \u7684\u8a2d\u5b9a\u3002\u5982\u679c\u4f60\u60f3\u8b93 NavigationBar \u7684\u80cc\u666f\u8b8a\u70ba\u900f\u660e\uff0c\u8acb\u6ce8\u610f\uff0c\u55ae\u7d14\u4f7f\u7528 `.backgroundColor = .clear` \u662f\u7121\u6cd5\u9054\u6210\u7684\u3002<\/p>\n<blockquote><p>\n  \u7121\u6cd5\u8b93 NavigationBar \u80cc\u666f\u900f\u660e\n<\/p><\/blockquote>\n<pre><code class=\"language-swift line-numbers\">UINavigationBar.appearance().backgroundColor = .clear\n<\/code><\/pre>\n<blockquote><p>\n  \u53ef\u4ee5\u8b93 NavigationBar \u80cc\u666f\u900f\u660e\n<\/p><\/blockquote>\n<pre><code class=\"language-swift line-numbers\">UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190918\/20112271jaijn2PE4J.png\" alt=\"\u8a2d\u5b9a\u900f\u660e\u80cc\u666f\u7684 NavigationBar\" title=\"\"><\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u53bb\u9664\u4e0b\u65b9\u7684\u5206\u9694\u7dda\uff0c\u53ef\u4ee5\u8a2d\u7f6e `shadowImage` \u4f86\u9054\u6210\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">UINavigationBar.appearance().shadowImage = UIImage()\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190918\/20112271JjHAwC8plt.png\" alt=\"\u53bb\u9664 NavigationBar \u4e0b\u65b9\u7684\u5206\u9694\u7dda\" title=\"\"><\/p>\n<h3>UITabBarController<\/h3>\n<p>\u5728\u4f7f\u7528 `UITabBarController` \u6642\uff0c\u5fc5\u9808\u5148\u5c07\u8981\u4f7f\u7528\u7684 Controller \u5305\u88dd\u5728 Navigation \u4e2d\uff0c\u7136\u5f8c\u5229\u7528 `self.addChild` \u65b9\u6cd5\u6dfb\u52a0\u9032\u53bb\u3002\u9019\u6a23\u5c31\u80fd\u540c\u6642\u64c1\u6709 TabBar \u548c NavigationBar \u7684\u529f\u80fd\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">override func viewDidLoad() {\n    super.viewDidLoad()\n    setUpChildViewControllers()\n    setUpTabBar()\n}\n<\/code><\/pre>\n<h4>setUpChildViewControllers<\/h4>\n<p>\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u5c07\u6309\u7167\u9806\u5e8f\u6dfb\u52a0 Child View Controllers\uff0c\u5305\u62ec `controller`\u3001`image`\uff08\u986f\u793a\u5716\u7247\uff09\u3001`selectedImage`\uff08\u6309\u4e0b\u6642\u986f\u793a\u5716\u7247\uff09\u548c `title`\uff08\u6a19\u984c\uff09\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func setUpChildViewControllers() {\n    addChildViewController(childController: Test1ViewController(), image: \"wallet\", selectedImage: \"wallet\", title: \"1\")\n    addChildViewController(childController: Test2ViewController(), image: \"wallet\", selectedImage: \"wallet\", title: \"2\")\n}\n<\/code><\/pre>\n<pre><code class=\"language-swift line-numbers\">func addChildViewController(childController: UIViewController, image: String, selectedImage: String, title: String) {\n    childController.title = title\n    childController.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(red: 176.0\/255.0, green: 196.0\/255.0, blue: 222.0\/255.0, alpha: 1)], for: .selected)\n    childController.tabBarItem.image = UIImage(named: image)\n    childController.tabBarItem.selectedImage = UIImage(named: selectedImage)\n    let navigationController = UINavigationController(rootViewController: childController)\n    navigationController.navigationBar.barTintColor = .white\n    self.addChild(navigationController)\n}\n<\/code><\/pre>\n<h4>setUpTabBar<\/h4>\n<p>\u9019\u500b\u65b9\u6cd5\u8ca0\u8cac\u8a2d\u7f6e\u81ea\u5b9a\u7fa9\u7684 TabBar\uff0c\u6240\u8a2d\u5b9a\u7684\u984f\u8272\u548c\u7279\u6548\u5c07\u6703\u751f\u6548\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func setUpTabBar() {\n    self.setValue(CustomTabBar(), forKey: \"tabBar\")\n}\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n\u5b8c\u6210\u6240\u6709\u8a2d\u7f6e\u5f8c\uff0c\u4f60\u7684\u61c9\u7528\u5c07\u64c1\u6709\u4e0a\u4e0b\u7684 Bar\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190918\/20112271xwCrTzMt2V.png\" alt=\"\u8a2d\u7f6e\u5b8c\u6210\u7684 TabBar \u548c NavigationBar\" title=\"\"><\/p>\n<h3>Uber Eats \u4eff\u88fd<\/h3>\n<p>\u4ee5\u4e0b\u662f\u6a21\u4eff Uber Eats \u61c9\u7528\u7684\u8a2d\u8a08\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190918\/20112271v9DThNokZh.png\" alt=\"Uber Eats \u61c9\u7528\u754c\u9762\" title=\"\"><\/p>\n<p>\u5728\u9019\u88e1\uff0c\u4e0a\u65b9\u7684 Bar \u80cc\u666f\u70ba\u767d\u8272\uff0c\u4e14\u4fdd\u7559\u4e0b\u65b9\u7684\u5e95\u7dda\u3002\u4e0b\u65b9\u7684\u56db\u500b icon \u548c title \u5168\u90e8\u96b1\u85cf\uff0c\u56e0\u70ba\u6211\u5011\u4f7f\u7528\u4e86\u622a\u5716\u4f86\u7372\u53d6 icon\u3002<\/p>\n<p><strong>\u539f\u751f<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190918\/20112271XcXjIxzKu4.png\" alt=\"\u539f\u751f\u61c9\u7528\u754c\u9762\" title=\"\"><br \/>\n<strong>\u4eff\u88fd<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190918\/20112271RPeTJQ73ZF.png\" alt=\"\u4eff\u88fd\u7684\u61c9\u7528\u754c\u9762\" title=\"\"><\/p>\n<hr \/>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<h4>1. \u5982\u4f55\u8b93 NavigationBar \u80cc\u666f\u900f\u660e\uff1f<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 `UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)` \u4f86\u8a2d\u7f6e\u900f\u660e\u80cc\u666f\u3002<\/p>\n<h4>2. \u70ba\u4ec0\u9ebc\u6211\u7684 TabBar \u4e0d\u986f\u793a\uff1f<\/h4>\n<p>\u8acb\u78ba\u4fdd\u4f60\u5df2\u7d93\u6b63\u78ba\u6dfb\u52a0\u4e86 Child View Controllers\uff0c\u4e26\u4e14\u8abf\u7528\u4e86 `setUpTabBar()` \u65b9\u6cd5\u3002<\/p>\n<h4>3. \u5982\u4f55\u81ea\u5b9a\u7fa9 TabBar \u7684\u984f\u8272\u548c\u6a23\u5f0f\uff1f<\/h4>\n<p>\u4f60\u53ef\u4ee5\u901a\u904e\u5275\u5efa\u4e00\u500b\u81ea\u5b9a\u7fa9\u7684 TabBar \u985e\u5225\u4f86\u8a2d\u7f6e TabBar \u7684\u984f\u8272\u548c\u6a23\u5f0f\uff0c\u4e26\u5728 `setUpTabBar()` \u65b9\u6cd5\u4e2d\u9032\u884c\u8a2d\u7f6e\u3002<br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0a\u4e00\u7bc7\u5df2\u7d93\u5b8c\u6210\u4e3b\u8981\u7684`TabBar`<br \/>\n\u63a5\u4e0b\u4f86\u628a`NavigationBar`\u8a2d\u5b9a\u597d<br \/>\n\u518d\u5229\u7528`TabBarController`\u628a\u5de6\u8180\u53f3\u81c2\u63a5\u8d77\u4f86<br \/>\n\u6574\u500b\u5e95\u5c64\u67b6\u69cb\u5c31\u57fa\u672c\u5b8c\u6574\u4e86<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-392","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-6k","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=392"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/392\/revisions"}],"predecessor-version":[{"id":579,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/392\/revisions\/579"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}