UIKit-cross-platform 弹窗与导航UIAlertController 与 UINavigationController 使用教程【免费下载链接】UIKit-cross-platformCross-platform Swift implementation of UIKit, mostly for Android项目地址: https://gitcode.com/gh_mirrors/ui/UIKit-cross-platform如果你正在寻找一套能让 Swift 代码在 Android 上原生运行的 UI 框架那么UIKit-cross-platform绝对值得关注。这是一个跨平台的 Swift UIKit 实现主打 Android 平台让你用熟悉的UIAlertController弹窗和UINavigationController导航组件一套代码同时跑通 iOS、macOS 与 Android。本文面向新手用最简单的方式带你掌握 UIKit-cross-platform 中弹窗与导航的完整用法快速构建出带交互的原生界面。为什么弹窗与导航是跨平台开发的第一步在移动应用里弹窗负责提示与确认导航负责页面跳转它们几乎出现在每一个 App 中。UIKit-cross-platform 最大的价值就是原封不动保留 iOS 开发习惯——你在 Xcode 里写的UIAlertController、UINavigationController代码稍作调整就能在 Android 上以原生性能运行不用重写两套逻辑。项目把官方 API 都集中在Sources/目录中例如 UIAlertController.swift 与 UINavigationController.swift源码清晰、注释友好非常适合边读边学。UIAlertController 弹窗使用入门创建警告框Alert的完整步骤警告框是最常见的弹窗用于提示信息或确认操作。在 UIKit-cross-platform 中创建方式与 iOS 完全一致let alert UIAlertController( title: 提示, message: 确定要删除这条记录吗, preferredStyle: .alert ) alert.addAction(UIAlertAction(title: 取消, style: .cancel, handler: nil)) alert.addAction(UIAlertAction(title: 删除, style: .destructive, handler: { _ in print(执行删除) })) self.present(alert, animated: true, completion: nil)关键点有三个用preferredStyle: .alert声明样式、用addAction添加按钮、用present弹出。是不是和 iOS 一模一样没错这正是该框架的核心优势。动作表单Action Sheet快速配置方法当你需要让用户从多个选项里选择时actionSheet样式会更合适let sheet UIAlertController(title: 选择操作, message: nil, preferredStyle: .actionSheet) sheet.addAction(UIAlertAction(title: 复制链接, style: .default, handler: nil)) sheet.addAction(UIAlertAction(title: 分享, style: .default, handler: nil)) sheet.addAction(UIAlertAction(title: 取消, style: .cancel, handler: nil)) self.present(sheet, animated: true, completion: nil)三种按钮样式怎么选UIAlertActionStyle提供三个选项选对样式能明显提升交互体验.default普通操作按钮如确定分享⚪️.cancel取消按钮通常置于底部表示放弃操作.destructive危险操作按钮如删除退出登录用于红色警示 小技巧从UIAlertControllerView.swift的布局逻辑可以看出按钮过少时会横向排列按钮较多时自动切换为纵向排列你无需手动处理布局。UINavigationController 导航栏使用教程初始化导航控制器的正确方法导航控制器负责管理页面栈View Controller Stack和顶部导航栏。创建方式非常直观let homeVC HomeViewController() let navController UINavigationController(rootViewController: homeVC) window.rootViewController navController在 UINavigationController.swift 的源码中可以看到根控制器会立刻被压入栈顶同时导航栏会自动更新。pushViewController 与 popViewController 使用技巧页面跳转依赖两个核心方法// 压入新页面跳转 navigationController?.pushViewController(DetailViewController(), animated: true) // 弹出当前页面返回 navigationController?.popViewController(animated: true)需要注意的细节栈底root页面不能被 pop源码中已做保护返回nil在 Android 上按系统返回键时UIWindowhardwareBackButton.swift 会自动帮你 pop 页面或退出弹窗交互体验非常贴近原生每个控制器的title属性会自动同步到导航栏标题无需额外设置设置导航栏标题与右侧按钮想要在导航栏右侧放一个完成按钮只需配置navigationItemoverride func viewDidLoad() { super.viewDidLoad() title 编辑资料 navigationItem.rightBarButtonItem UIBarButtonItemWithClosure( barButtonSystemItem: .done ) { print(点击了完成) } }UINavigationItem.swift 提供了rightBarButtonItem与setRightBarButton(_:animated:)接口目前支持.done系统按钮足够覆盖大多数业务场景。弹窗 导航组合使用的实战示例把两者结合是最常见的真实需求列表页 → 点击进入详情 → 详情页弹出确认框。项目自带的 Demo 就展示了完整流程参考 samples/getting-started/DemoApp/ViewController.swiftfunc presentAlertController() { let alertController UIAlertController( title: Alert Message, message: 这是一条弹窗提示, preferredStyle: .alert ) alertController.addAction(UIAlertAction(title: Ok, style: .default, handler: nil)) alertController.addAction(UIAlertAction(title: Cancel, style: .destructive, handler: nil)) self.present(alertController, animated: true, completion: nil) }想要亲自体验运行 DemoApp 即可在 iOS、macOS、Android 三种平台看到弹窗与导航效果相关工程配置位于samples/getting-started/目录下。新手常见问题与避坑指南弹窗未显示检查是否调用了present(_:animated:completion:)且不要在已经 present 了其他控制器的情况下再次 present源码会打印警告并忽略请求。导航栏返回按钮不见了在 Android 上返回按钮默认显示点击栈顶以外页面时执行 pop栈底页面则触发 dismiss行为与 iOS 略有差异属预期设计。页面空白确保UINavigationController栈中至少有一个控制器否则会触发断言失败。快速上手的 3 个建议先跑 Demo克隆仓库后直接运行samples/getting-started下的示例感受三端一致的交互阅读源码注释Sources/下每个文件都保留了详细的实现说明是学习跨平台 UI 设计的绝佳教材从弹窗开始练手弹窗代码量小、见效快用它建立信心后再挑战导航栈与复杂页面UIKit-cross-platform 让一次编写、处处运行从口号变成了现实。现在就从创建第一个UIAlertController弹窗开始体验 Swift 代码在 Android 上原生运行的乐趣吧【免费下载链接】UIKit-cross-platformCross-platform Swift implementation of UIKit, mostly for Android项目地址: https://gitcode.com/gh_mirrors/ui/UIKit-cross-platform创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考