{"id":390,"date":"2025-05-30T10:54:58","date_gmt":"2025-06-05T03:25:57","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=390"},"modified":"2025-06-05T10:54:58","modified_gmt":"2025-06-05T03:25:57","slug":"swift-%e4%bd%bf%e7%94%a8tabbarnavigationbar-%e7%af%84%e4%be%8b-%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e4%bd%bf%e7%94%a8tabbarnavigationbar-%e7%af%84%e4%be%8b-%e4%b8%8a\/","title":{"rendered":"Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u4f7f\u7528 TabBar \u8207 NavigationBar \u7684\u73fe\u4ee3\u7bc4\u4f8b (2025 \u6700\u65b0)"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h3>\u7c21\u8981<\/h3>\n<p>\u5728\u6bcf\u500b iOS \u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c<code>TabBar<\/code> \u548c <code>NavigationBar<\/code> \u662f\u6700\u57fa\u672c\u7684\u7d44\u4ef6\uff0c\u5916\u9001\u5e73\u53f0\u7684\u5e95\u5c64\u67b6\u69cb\u4e5f\u4e0d\u4f8b\u5916\u3002\u9019\u5169\u500b Bar \u53ef\u4ee5\u6839\u64da\u9700\u6c42\u986f\u793a\u6216\u96b1\u85cf\u3002\u70ba\u4e86\u907f\u514d\u5728 <code>AppDelegate<\/code> \u4e2d\u7684\u7a0b\u5f0f\u78bc\u904e\u65bc\u96dc\u4e82\uff0c\u6211\u5011\u5c07\u5176\u5206\u70ba\u66f4\u7d30\u7684\u7d50\u69cb\uff1a<code>TabBarController<\/code>\u3001<code>TabBar<\/code> \u548c <code>NavigationController<\/code>\uff0c\u7136\u5f8c\u518d\u7531 <code>AppDelegate<\/code> \u9032\u884c\u547c\u53eb\u3002\u9019\u6a23\u7684\u7d44\u7e54\u7d50\u69cb\u6703\u66f4\u6e05\u6670\u3002<\/p>\n<h3>\u74b0\u5883\u8a2d\u5b9a<\/h3>\n<p>\u9996\u5148\uff0c\u8acb\u5efa\u7acb <code>TabBarController<\/code>\u3001<code>TabBar<\/code> \u548c <code>NavigationController<\/code> \u4e09\u500b\u985e\u5225\uff0c\u60a8\u53ef\u4ee5\u81ea\u7531\u547d\u540d\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190917\/20112271SwBjCi492n.png\" alt=\"TabBar \u548c NavigationBar \u7684\u7d50\u69cb\u793a\u610f\u5716\" title=\"\"><\/p>\n<h3>AppDelegate \u8a2d\u5b9a<\/h3>\n<p>\u5728 <code>AppDelegate<\/code> \u4e2d\uff0c\u5ba3\u544a\u525b\u525b\u5efa\u7acb\u597d\u7684 <code>TabBarController<\/code>\uff0c\u4e26\u5c07\u5176\u8a2d\u7f6e\u70ba <code>rootViewController<\/code>\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {\n    let tabBarController = JGTabBarController()\n    self.window?.rootViewController = tabBarController\n    return true\n}\n<\/code><\/pre>\n<h3>\u8a2d\u7f6e UITabBar<\/h3>\n<p>\u5728\u8a2d\u5b9a <code>UITabBar<\/code> \u7684 <code>backgroundColor<\/code> \u6642\uff0c\u82e5\u76f4\u63a5\u4f7f\u7528 <code>self.backgroundColor<\/code>\uff0c\u984f\u8272\u53ef\u80fd\u6703\u8207\u9810\u671f\u4e0d\u540c\u3002\u76f8\u5c0d\u5730\uff0c\u4f7f\u7528 <code>self.backgroundImage<\/code> \u8a2d\u7f6e\u984f\u8272\u6703\u66f4\u70ba\u6e96\u78ba\u3002<\/p>\n<h4>\u4f7f\u7528 self.backgroundColor<\/h4>\n<pre><code class=\"language-swift line-numbers\">self.backgroundColor = UIColor(displayP3Red: 0.1, green: 0.4, blue: 0.9, alpha: 0.8)\n<\/code><\/pre>\n<p><strong>\u5448\u73fe\u5716<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190917\/20112271Vv66L5oyMk.png\" alt=\"UITabBar \u80cc\u666f\u984f\u8272\u8a2d\u5b9a\" title=\"\"><\/p>\n<h4>\u4f7f\u7528 self.backgroundImage<\/h4>\n<pre><code class=\"language-swift line-numbers\">let transparentBlackColor = UIColor(displayP3Red: 0.1, green: 0.4, blue: 0.9, alpha: 0.8)\nlet rect = CGRect(x: 0, y: 0, width: 1, height: 1)\nUIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)\ntransparentBlackColor.setFill()\nUIRectFill(rect)\nif let image = UIGraphicsGetImageFromCurrentImageContext() {\n    self.backgroundImage = image\n}\nUIGraphicsEndImageContext()\n<\/code><\/pre>\n<p><strong>\u5448\u73fe\u5716<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/ithelp.ithome.com.tw\/upload\/images\/20190917\/20112271oCNeWbXHGM.png\" alt=\"\u5229\u7528 UIGraphics \u751f\u6210\u80cc\u666f\u5716\" title=\"\"><\/p>\n<blockquote><p>\n  \u5728\u4f7f\u7528 <code>self.backgroundColor<\/code> \u6642\uff0c\u53ef\u80fd\u6703\u51fa\u73fe\u4e00\u5c64\u900f\u660e\u7684\u767d\u8272\u8986\u84cb\uff0c\u9019\u53ef\u80fd\u9700\u8981\u8abf\u6574\u5176\u4ed6\u8b8a\u6578\u6216\u96b1\u85cf\u67d0\u4e9b\u7269\u4ef6\u3002\u9019\u500b\u554f\u984c\u53ef\u4ee5\u5728\u672a\u4f86\u9032\u4e00\u6b65\u8655\u7406\u3002\n<\/p><\/blockquote>\n<h3>UITabBar \u9032\u968e\u529f\u80fd<\/h3>\n<p>\u5982\u679c\u5e0c\u671b\u8b93 TabBar \u66f4\u5177\u4e92\u52d5\u6027\uff0c\u53ef\u4ee5\u5728 <code>UITabBar<\/code> \u7684 <code>layoutSubviews<\/code> \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u7279\u6548\u6216\u5176\u4ed6 <code>\u9ede\u64ca\u4e8b\u4ef6<\/code> \u7684\u8655\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8b93 <code>Button<\/code> \u5728\u9ede\u64ca\u6642\u8f15\u5fae\u8df3\u52d5\uff0c\u589e\u5f37\u7528\u6236\u4e92\u52d5\u9ad4\u9a57\u3002<\/p>\n<h4>layoutSubviews<\/h4>\n<p>\u63a7\u5236\u6bcf\u500b <code>Button<\/code> \u7684\u9ede\u64ca\u4e8b\u4ef6\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">override func layoutSubviews() {\n    super.layoutSubviews()\n    var tabBarBtnIndex = 0\n    for subView in self.subviews {\n        if let navButtonClass = NSClassFromString(\"UITabBarButton\") {\n            if subView.isKind(of: navButtonClass) {\n                tabBarBtnIndex += 1\n                print(tabBarBtnIndex)\n\n                let tabBarBtn = subView as? UIControl\n                tabBarBtn!.addTarget(self, action: #selector(buttonClicked(tabBarButton:)), for: .touchUpInside)\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h4>buttonClicked<\/h4>\n<p>\u7576\u7528\u6236\u9ede\u64ca <code>Button<\/code> \u6642\uff0c\u89f8\u767c <code>touchUpInside<\/code> \u4e8b\u4ef6\uff0c\u6240\u89f8\u767c\u7684 <code>Button<\/code> \u5c07\u5728\u6307\u5b9a\u7684 <code>duration<\/code> \u6642\u9593\u5167\u5be6\u73fe\u653e\u5927\u7e2e\u5c0f\u7279\u6548\uff0c\u8a72\u7279\u6548\u7531 <code>animation.values<\/code> \u63a7\u5236\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">@IBAction func buttonClicked(tabBarButton: UIControl) {\n    for imageView in tabBarButton.subviews {\n        if let navButtonClass = NSClassFromString(\"UITabBarSwappableImageView\") {\n            if imageView.isKind(of: navButtonClass) {\n                let animation = CAKeyframeAnimation(keyPath: \"transform.scale\")\n                animation.values = [1.0, 1.15, 1.0]\n                animation.duration = 0.2\n                animation.calculationMode = .cubic\n                imageView.layer.add(animation, forKey: nil)\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p><strong>Demo<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/i.imgur.com\/OpSvFBC.gif\" alt=\"TabBar \u4e92\u52d5\u6548\u679c\u793a\u7bc4\" title=\"\"><\/p>\n<hr \/>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"\u9ede\u64ca\u9019\u88e1\u4e86\u89e3\u66f4\u591a\" title=\"\"><\/a><\/p>\n<h3>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h3>\n<h4>1. \u5982\u4f55\u5728 TabBar \u4e2d\u6dfb\u52a0\u81ea\u5b9a\u7fa9\u6309\u9215\uff1f<\/h4>\n<p>\u60a8\u53ef\u4ee5\u5728 <code>layoutSubviews<\/code> \u65b9\u6cd5\u4e2d\u81ea\u5b9a\u7fa9\u6dfb\u52a0\u6309\u9215\uff0c\u4e26\u70ba\u5176\u8a2d\u7f6e\u76ee\u6a19\u884c\u70ba\u3002<\/p>\n<h4>2. \u5982\u4f55\u8655\u7406 TabBar \u7684\u9ede\u64ca\u4e8b\u4ef6\uff1f<\/h4>\n<p>\u4f7f\u7528 <code>addTarget(_:action:for:)<\/code> \u65b9\u6cd5\u70ba\u6bcf\u500b TabBar Button \u8a2d\u7f6e\u89f8\u767c\u4e8b\u4ef6\u3002<\/p>\n<h4>3. \u5982\u4f55\u8abf\u6574 TabBar \u7684\u5916\u89c0\uff1f<\/h4>\n<p>\u53ef\u4ee5\u901a\u904e\u4fee\u6539 <code>backgroundColor<\/code> \u548c <code>backgroundImage<\/code> \u4f86\u6539\u8b8a TabBar \u7684\u5916\u89c0\uff0c\u4e26\u4f7f\u7528\u81ea\u5b9a\u7fa9\u7684\u5716\u50cf\u4f86\u63d0\u5347\u8996\u89ba\u6548\u679c\u3002<br \/>\n&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6bcf\u500bAPP\u6700\u57fa\u672c\u90fd\u6703\u904b\u7528\u5230 `TabBar`+`NavigationBar`<br \/>\n\u5916\u9001\u5e73\u53f0\u4e5f\u4e0d\u4f8b\u5916 \u57fa\u672c\u4e0a\u5e95\u5c64\u67b6\u69cb\u90fd\u5dee\u4e0d\u591a<br \/>\n\u4e0a\u4e0b\u7684Bar \u6709\u6642\u6709 \u6709\u6642\u96b1\u85cf\u7684\u904b\u7528<br \/>\n\u70ba\u4e86\u5fc5\u514d\u5168\u90fd\u5beb\u5728`AppDelegate`\u592a\u904e\u96dc\u4e82<br \/>\n\u6211\u5011\u9019\u908a\u5206\u70ba\u66f4\u7d30 `TabBarController`+`TabBar`+`NavigationController`<br \/>\n\u7136\u5f8c\u518d\u7d66`AppDelegate`\u505a\u547c\u53eb<br \/>\n\u9019\u6a23\u61c9\u8a72\u6574\u7406\u7684\u4e0d\u932f<\/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-390","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-6i","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/390","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=390"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/390\/revisions"}],"predecessor-version":[{"id":580,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/390\/revisions\/580"}],"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=390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}