在Visual Studio 17的Xamarin中为iOS创建全局自定义工具栏
嘿,刚好能帮你解决这个问题!在Visual Studio 17的iOS项目里,现在确实默认不再用Main.storyboard了,都是用代码来配置场景和窗口。下面是一步步实现全局自定义工具栏(带应用名和图标,这里默认是顶部导航栏样式,要是需要底部工具栏我也会补充说明)的方法:
实现全局自定义工具栏步骤
1. 创建自定义导航栏类
首先新建一个CustomNavigationBar.swift文件,我们在这里定义工具栏的样式、图标和标题:
import UIKit class CustomNavigationBar: UINavigationBar { override init(frame: CGRect) { super.init(frame: frame) setupCustomStyle() } required init?(coder: NSCoder) { super.init(coder: coder) setupCustomStyle() } private func setupCustomStyle() { // 设置工具栏背景色,你可以改成自己想要的颜色 backgroundColor = .systemMint // 去掉默认的阴影线 shadowImage = UIImage() // 配置应用图标 let appIconView = UIImageView(image: UIImage(named: "AppIcon")) appIconView.contentMode = .scaleAspectFit appIconView.frame = CGRect(x: 0, y: 0, width: 32, height: 32) let iconBarItem = UIBarButtonItem(customView: appIconView) // 配置应用标题 let titleLabel = UILabel() titleLabel.text = "你的应用名称" titleLabel.textColor = .white titleLabel.font = UIFont.systemFont(ofSize: 19, weight: .semibold) // 把图标和标题加到导航栏上 self.topItem?.leftBarButtonItem = iconBarItem self.topItem?.titleView = titleLabel } // 给个公共方法,方便后续页面动态修改标题 func updateTitle(_ newTitle: String) { if let titleView = self.topItem?.titleView as? UILabel { titleView.text = newTitle } } }
2. 在全局场景配置中应用自定义工具栏
打开SceneDelegate.swift,在初始化窗口的方法里,给所有导航控制器指定使用我们的自定义导航栏:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) self.window = window // 示例:创建根导航控制器,并指定自定义导航栏 let homeVC = UIViewController() homeVC.view.backgroundColor = .white // 关键:初始化导航控制器时传入自定义导航栏类 let rootNavVC = UINavigationController(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil) rootNavVC.viewControllers = [homeVC] window.rootViewController = rootNavVC window.makeKeyAndVisible() }
3. 确保所有页面自动继承样式
只要后续你添加的页面都是通过这个根导航控制器push进去的,或者放在TabBar的子导航控制器里,都会自动使用这个自定义工具栏。比如如果用TabBar的话,每个Tab对应的导航控制器都要指定自定义栏:
// 示例TabBar配置 let tabBarVC = UITabBarController() // 第一个Tab let firstVC = UIViewController() let firstNavVC = UINavigationController(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil) firstNavVC.viewControllers = [firstVC] firstNavVC.tabBarItem = UITabBarItem(title: "首页", image: UIImage(named: "home"), tag: 0) // 第二个Tab let secondVC = UIViewController() let secondNavVC = UINavigationController(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil) secondNavVC.viewControllers = [secondVC] secondNavVC.tabBarItem = UITabBarItem(title: "我的", image: UIImage(named: "profile"), tag: 1) tabBarVC.viewControllers = [firstNavVC, secondNavVC] window.rootViewController = tabBarVC
4. 如果你需要的是底部工具栏
要是你要的是底部的工具栏,那可以创建CustomToolbar.swift,代码类似:
import UIKit class CustomToolbar: UIToolbar { override init(frame: CGRect) { super.init(frame: frame) setupCustomStyle() } required init?(coder: NSCoder) { super.init(coder: coder) setupCustomStyle() } private func setupCustomStyle() { backgroundColor = .systemMint // 添加图标和标题项 let appIconItem = UIBarButtonItem(image: UIImage(named: "AppIcon"), style: .plain, target: nil, action: nil) let titleItem = UIBarButtonItem(title: "你的应用名称", style: .plain, target: nil, action: nil) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) self.items = [appIconItem, flexibleSpace, titleItem, flexibleSpace] } }
然后在导航控制器初始化时传入toolbarClass: CustomToolbar.self,或者在页面里设置navigationController?.toolbar.isHidden = false就能显示了。
小提示
- 记得把你的应用图标添加到项目的
Assets.xcassets里,代码里的图标名称要和Assets里的一致哦 - 如果要在某个页面单独修改工具栏样式,可以在
viewWillAppear里做调整,不会影响全局
内容的提问来源于stack exchange,提问作者Benz Drogba
相关产品推荐
相关产品推荐

