swift - 为什么这不打开导航控制器窗口?
问题描述
为什么按下相机按钮时以下代码没有打开导航控制器?done 函数指定相机应该推送视图控制器,但没有任何反应。
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screenSize.width, height: 160))
let navItem = UINavigationItem(title: "Hello")
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: nil, action: #selector(done))
navItem.rightBarButtonItem = doneItem
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
@objc func done() { // remove @objc for Swift 3
let dummyViewController = UIViewController()
navigationController?.pushViewController(dummyViewController, animated: true)
}
这是 AppDelagate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
FirebaseApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController =
UINavigationController(rootViewController: MessagesController())
UITableViewCell.appearance().textLabel?.textColor = UIColor.white;
UITableViewCell.appearance().backgroundColor = UIColor.clear
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
UINavigationBar.appearance().barTintColor = UIColor(red:0.13, green:0.15, blue:0.18, alpha:1.0)
UIApplication.shared.isStatusBarHidden = false
return true
}
func applicationWillResignActive(_ application: UIApplication) {
let im = UIImageView()
im.tag = 12
im.frame = (self.window?.frame)!
im.image = UIImage.init(named: "launchbackground.png")
application.keyWindow?.subviews.last?.addSubview(im)
}
func applicationDidEnterBackground(_ application: UIApplication) {
let im = UIImageView()
im.tag = 12
im.frame = (self.window?.frame)!
im.image = UIImage.init(named: "launchbackground.png")
application.keyWindow?.subviews.last?.addSubview(im)
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let sub = application.keyWindow?.subviews.last
{
for vv in sub.subviews
{
if(vv.tag == 12)
{
vv.removeFromSuperview()
}
}
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
if let sub = application.keyWindow?.subviews.last
{
for vv in sub.subviews
{
if(vv.tag == 12)
{
vv.removeFromSuperview()
}
}
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
还有什么我可以添加或删除来帮助解决这个问题的,欢迎提出任何建议。
更新:
所以基本上我有一个根视图控制器,它只在用户登录后显示。在用户登录之前,他们会看到另一个我正在尝试构建的视图控制器;这个视图控制器是一个注册页面,我需要在顶部有一个导航栏,并带有“登录”选项。计划是让用户点击“登录”导航控件并显示导航控制器(即从右边)显示登录表单。
解决方案
如果您只是想在导航栏中放置一个相机按钮来推动另一个视图控制器,那么有一种更简单的方法可以做到这一点:
func setNavigationBar() { let saveButton = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(done)) navigationItem.rightBarButtonItem = saveButton } @objc func done() { // remove @objc for Swift 3 let dummyViewController = UIViewController() navigationController?.pushViewController(dummyViewController, animated: true) }
编辑
所以,如果我理解得很好,您正在展示一个注册页面视图控制器,而这就是您需要导航按钮来导航到下一个视图的地方,对吗?问题可能是你如何“呈现”这个视图控制器。如果您使用方法 present(_:animated:completion:),视图将以模态方式呈现,这意味着它不会有导航控制器或导航栏。
为了解决这个问题,您可以创建一个 UINavigationController,将您的注册页面设置为 rootViewController,并显示导航控制器。
尝试这个:
let registerViewController = RegisterViewController()
let registerNavigationController = UINavigationController(rootViewController: registerViewController)
currentViewController.present(registerNavigationController, animated: true)
推荐阅读
- ios - 为什么 PDFKit .beginFindStrings 的结果为零?
- git - git:删除未使用的分支及其提交
- python - 如何在不关闭图形的情况下清除所有子图?
- c# - Core 3.0 中的 Process.Start 不会仅按名称打开文件夹
- python - 你能同时循环一个熊猫df和数组吗?
- rust - 在期货 0.3 中找不到 and_then 方法
- java - 我无法使用 graphql-java 上传文件
- r - Remove NAs after pivot_wider to match up rows
- css - 使用 CSS 为小屏幕重新格式化表格 - 将列保持在一起
- ffmpeg - 视频与旧的一起使用新的宽度 + 高度进行编码。我可以使用 ffmpeg 仅使用旧尺寸重新编码吗?