首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >斯威夫特5: UINavigationController要到DetailsViewController才会出现

斯威夫特5: UINavigationController要到DetailsViewController才会出现
EN

Stack Overflow用户
提问于 2020-06-04 18:37:19
回答 1查看 173关注 0票数 1

我正在以编程的方式重建我的应用程序,而不是使用故事板,我的UINavigationController会遇到问题。当应用程序第一次加载根ViewController时,包含“载体”和时间的条形图是我指定的颜色的轻量级版本,搜索栏存在,而不是普通的导航栏。

当我点击/单击UITableView上的一个单元格以到达详细信息屏幕,然后返回时,我的UINavigationBar将正确显示,但搜索栏将消失。我肯定我混淆了什么东西,但我不知道我在哪里搞砸了,或者我是不是完全错过了什么。任何和所有的帮助都将不胜感激。我还包括了一个图片,作为一个例子,发生了什么。

Nonexistant UINavigationController > Detail screen > Back.gif

我在TabBarController SceneDelegate 中设置了一个

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// MARK: - Properties
var window: UIWindow?
let tabBarDelegate = TabBarDelegate()
let userAuthToken = UserDefaults.standard.string(forKey: "token")
let userKeyToken = UserDefaults.standard.string(forKey: "key")


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

    // Disable dark mode
    if #available(iOS 13.0, *) {
        window?.overrideUserInterfaceStyle = .light
    }

    // IF USER IS LOGGED IN
    if let windowScene = (scene as? UIWindowScene) {

        if let _ = userAuthToken {
            self.window = UIWindow(windowScene: windowScene)

            // CREATE TAB BAR //
            let tabController = UITabBarController()

            tabController.tabBar.backgroundColor = .white

            // Instantiate the storyboards
            let cannabisStoryboard = UIStoryboard(name: "Cannabis", bundle: nil)
            let profileStoryboard = UIStoryboard(name: "Profile", bundle: nil)


            // Instantiate the view controllers to storyboards
            let cannabisVC = cannabisStoryboard.instantiateViewController(withIdentifier: "Cannabis") as! CannabisViewController
            let profileVC = profileStoryboard.instantiateViewController(withIdentifier: "Profile") as! ProfileViewController

            // Displays the items in below order in tab bar
            let vcData: [(UIViewController, UIImage, UIImage)] = [
                (cannabisVC, UIImage(named: "Cannabis_icon")!, UIImage(named: "Cannabis_icon_selected")!),
                (profileVC, UIImage(named: "Profile_icon")!, UIImage(named: "Profile_icon_selected")!),
            ]

            let vcs = vcData.map { (vc, defaultImage, selectedImage) -> UINavigationController in
                let nav = UINavigationController(rootViewController: vc)
                nav.tabBarItem.image = defaultImage
                nav.tabBarItem.selectedImage = selectedImage

                return nav
            }

            // Assign to tab bar controller
            tabController.viewControllers = vcs
            tabController.tabBar.isTranslucent = false
            tabController.delegate = tabBarDelegate

            // Disables rendering for tab bar images
            if let items = tabController.tabBar.items {
                for item in items {
                    if let image = item.image {
                        item.image = image.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    }

                    if let selectedImage = item.selectedImage {
                        item.selectedImage = selectedImage.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
                    }

                    // Hides title
                    item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
                }
            }

            // Customize Navigation bar
            UINavigationBar.appearance().backgroundColor = UIColor(rgb: 0x00ffcc)

            // Disable dark mode
            window!.overrideUserInterfaceStyle = .light

            window?.rootViewController = tabController
            window?.makeKeyAndVisible()



        } else {
            // let loginStoryboard = UIStoryboard(name: "Login", bundle: nil)
            // let loginViewController = loginStoryboard.instantiateViewController(withIdentifier: "Login") as! LoginViewController

            // Disable dark mode
            window!.overrideUserInterfaceStyle = .light

            // window?.rootViewController = loginViewController
            window?.rootViewController = LoginViewController()
            window?.makeKeyAndVisible()
        }



        window?.windowScene = windowScene
    }

}

Root ViewController (简化为相关信息)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class CannabisViewController: UIViewController {

// MARK:- Outlets
let tableView = UITableView()

// MARK:- Properties
var cannabisDetailViewController: CannabisDetailsViewController? = nil


// Search
let searchController = UISearchController(searchResultsController: nil)
var isSearchBarEmpty: Bool { return searchController.searchBar.text?.isEmpty ?? true }
var filteredStrains = [Cannabis]()
var isFiltering: Bool { return searchController.isActive && !isSearchBarEmpty }


// MARK: - ViewWillLayoutSubViews
override func viewWillLayoutSubviews() {
    let navigationBar: UINavigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44))

    // navigationController?.setViewControllers([CannabisViewController()], animated: true)


    self.view.addSubview(navigationBar)
}

// MARK: - ViewDidLoad
override func viewDidLoad() {
    super.viewDidLoad()

    configurePage()


    // MARK: Self sizing table view cell
    tableView.estimatedRowHeight = CGFloat(88.0)
    tableView.rowHeight = UITableView.automaticDimension



    // MARK: DataSource/Delegate
    tableView.dataSource = self
    tableView.delegate = self


    // Removes default lines from table views
    tableView.tableFooterView = UIView()
    tableView.separatorStyle = .none


    // MARK: Navigation: logo in center
    let logoHeader = UIImageView(image: UIImage(named: "logoHeader"))
    self.navigationItem.titleView = logoHeader


    // MARK: API
    getCannabisList()


    // MARK: Search bar controller
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search for a strain!"
    navigationItem.searchController = searchController
    definesPresentationContext = true

}


// Configure TableView
func configurePage() {
    // Configure Tableview
    view.addSubview(tableView)
    tableView.anchor(top: view.topAnchor, left: view.leftAnchor,
                     bottom: view.bottomAnchor, right: view.rightAnchor)
}

}

SearchController (仍在根ViewController中)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    func updateSearchResults(for searchController: UISearchController) {
    let searchBar = searchController.searchBar
    let userSearch = searchBar.text!.trimmingCharacters(in: .whitespaces)
    search(searchText: userSearch)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-04 19:20:57

我可以在这里看到几个问题,但是首先要在SceneDelegate中初始化窗口,您可以使用UIWindowScene

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
var window: UIWindow?

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 rootViewController = RootViewController()
    window.rootViewController = UINavigationController(rootViewController: rootViewController)
    window.makeKeyAndVisible()
}

如果您想在应用程序中全局禁用黑暗模式,只需将一个键UIUserInterfaceStyle添加到Info.plist中,并将其值设置为Dark (或Light)。通过这样做,您将不需要更新每个视图控制器,因为它将覆盖全局应用程序默认样式。我强烈鼓励你增加对黑暗模式的支持!

如果要更改导航栏外观:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
if #available(iOS 13.0, *) {
    let navigationAppearance = UINavigationBarAppearance()
    navigationAppearance.configureWithOpaqueBackground()
    navigationAppearance.backgroundColor = .white
    navigationAppearance.titleTextAttributes = // ...
    navigationAppearance.largeTitleTextAttributes = // ...
    UINavigationBar.appearance().tintColor = .systemBlue
    UINavigationBar.appearance().barTintColor = .white
    UINavigationBar.appearance().standardAppearance = navigationAppearance
    UINavigationBar.appearance().scrollEdgeAppearance = navigationAppearance
} else {
    UINavigationBar.appearance().backgroundColor = .white
    UINavigationBar.appearance().barTintColor = .white
    UINavigationBar.appearance().tintColor = .systemBlue
    UINavigationBar.appearance().titleTextAttributes = // ...
    UINavigationBar.appearance().largeTitleTextAttributes = // ...
}

我做了一个最小的项目,让您看到一个使用搜索栏的工作示例,在这个示例中,状态栏的闪烁不会发生,UISearchBar的隐藏/显示动画在推送/弹出DetailViewController时正常工作。

RootViewController:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import UIKit

class RootViewController: UIViewController {

    private let reuseIdentifier = "reuseIdentifier"

    lazy var tableView: UITableView = {
        $0.delegate = self
        $0.dataSource = self
        $0.register(UITableViewCell.self, forCellReuseIdentifier: reuseIdentifier)
        return $0
    }(UITableView(frame: .zero, style: .grouped))

    private lazy var searchController: UISearchController = {
        $0.searchResultsUpdater = self
        $0.delegate = self
        $0.searchBar.delegate = self
        $0.obscuresBackgroundDuringPresentation = false
        $0.hidesNavigationBarDuringPresentation = false
        $0.searchBar.backgroundColor = .white
        $0.searchBar.tintColor = .systemBlue
        return $0
    }(UISearchController(searchResultsController: nil))

    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        setupConstraints()
    }

    func setupViews() {
        title = "Source"
        view.backgroundColor = .white
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
        view.addSubview(tableView)
        // ...
    }

    func setupConstraints() {
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    }
}

extension RootViewController: UITableViewDelegate {

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailViewController = DetailViewController()
        navigationController?.pushViewController(detailViewController, animated: true)
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNonzeroMagnitude
    }
}

extension RootViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier, for: indexPath)
        cell.textLabel?.text = "Cell at indexPath \(indexPath)"
        return cell
    }
}

extension RootViewController: UISearchResultsUpdating, UISearchControllerDelegate, UISearchBarDelegate {
    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {}
    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {}
    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {}
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {}
    func updateSearchResults(for searchController: UISearchController) {}
}

DetailViewController:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
class DetailViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Detail"
        view.backgroundColor = .systemGray6
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62207213

复制
相关文章
UINavigationController
UINavigationController 利用UINavigationController,可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是系统自带的“设置”应用 UINavigationController的使用步骤 初始化UINavigationController 设置UIWindow的rootViewController为UINavigationController 根据具体情况,通过push方法添加对应个数的子控制器 UINavigationController以栈的形
用户1941540
2018/05/11
1.4K0
UINavigationController的setViewControllers方法
在iOS开发中,UINavigationController是很常用的Controller,对它的一般操作就像操作一个栈,push和pop。但也经常会遇到pop和push无法优雅的完成的操作,比如退回到中间的某个VC上,或者在第一个VC之前添加一个VC等,更甚者要重新构造整个VC的顺序,这时候setViewControllers方法就排上用场了,它使对VC栈的操作不再局限于push和pop,而是构造整个VC栈并应用到当前的UINavigationController中,这个方法支持iOS3.0+,放心使用。 #Sample
JoeyBlue
2021/09/07
8750
iOS 8 之后UINavigationController新特性
iOS 8 之后,UINavigationController 为开发者提供了一些好用的功能,这些功能以前实现起来可能比较麻烦,而现在只需要一个属性就搞定了。
YungFan
2018/09/19
9210
iOS 8 之后UINavigationController新特性
IOS 导航栏 UINavigationController 常用
1 创建:FirstViewController、SecondViewController 2、在FirstViewController的viewDidLoad设置属性 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.title = “第一页” self.view.backgroundColor = UIColor.brown self.navigationItem.rightBarButtonItem = UIBarButtonItem(title:”下一页”, style: UIBarButtonItemStyle.plain, target:self, action:
用户5760343
2019/07/07
1.1K0
UI篇-UINavigationController之易忘补充
[UIApplication sharedApplication].statusBarHidden = YES;
進无尽
2018/09/12
2.2K0
UI篇-UINavigationController之易忘补充
只有程序员才会笑破肚子
Java开发教程视频 关注我们,领取500G开发教程视频 今天也是周末了,给大家分享一些好玩的趣图,喜欢大家乐呵一下,不是程序员的是不能理解图中所表示的意思的,就算看得懂也没有亲身体会的感觉,好了话不多说,直接分享了!资源的话,点击阅读全文就能看到啦 ①程序猿最烦两件事,第一件事是别人要他给自己的代码写文档,第二件呢?是别人的程序没有留下文档。 ②程序猿的读书历程:x 语言入门 — x 语言应用实践 — x 语言高阶编程 — x 语言的科学与艺术 — 编程之美 — 编程之道 — 编程之禅
七月半夏
2018/06/29
5030
iOS多控制器之UINavigationController&UITableBarController1. 多控制器2. UINavigationController3. UITableBarCont
1. 多控制器 一个iOS的app很少只由一个控制器组成,除非这个app极其简单 当app中有多个控制器的时候,我们就需要对这些控制器进行管理 有多个view时,可以用一个大的view去管理1个或者多个小view 控制器也是如此,用1个控制器去管理其他多个控制器 比如,用一个控制器A去管理3个控制器B、C、D 控制器A被称为控制器B、C、D的“父控制器” 控制器B、C、D的被称为控制器A的“子控制器” 为了便于管理控制器,iOS提供了2个比较特殊的控制器: UINavigationControl
stanbai
2018/06/28
1.4K0
​小长假要到了,来偶遇吗?
风里雨里,我在深圳机场等你,口说无凭,上图! 这是一段很长的故事!以前倒也不曾提过~ 银河证券和腾讯云数据库长久以来并肩作战,情比金坚,我们的故事日前在深圳宝安机场上映 他说:做好国产化分布式改造,就用腾讯云数据库。 我说:做国产数据库,我是认真的。 诞生于2007年的腾讯云数据库现已历经十四年的锤炼,走过自研业务的孵化,开启全面开放策略,随后进入上云时代,再到深入金融核心。如今,腾讯云数据库在公有云、 私有云领域提供全行业数据库解决方案,支持云上、云下及混合云部署模式,现已深入到千行百业,累计服务超
腾讯云数据库 TencentDB
2022/09/27
6880
​小长假要到了,来偶遇吗?
UINavigationController 导航控制器概念属性方法
概念 UINavigationController 继承于 UIViewController 包含:viewcontrollers、NavigationBar、Toolbar 导航控制器是一个堆栈结构,只是其中管理的对象是controller,通过push与pop进行controller的切换,UINavigationController是将这些控件(UINavigationBar,UINavigationItem和UIToolBar)和UIViewController紧密的结合了起来 总结: Naviga
用户2141756
2018/05/18
2.2K0
只有会编程的人才会用goto~
学习编程的时候有没有一位心灵导师告诉过你,编程的时候千万不要使用goto,否则他会给你带来意想不到的后果。我也是,看过的所有初学者的书上都在贯穿着这一思想,我不明白,为什么大家都不让使用goto语句,如果真的像大家说的那样恐怖,那么为什么goto语句至今还没有被移除出去。
CPP开发前沿
2022/06/04
8490
这家公司要到太空采矿了!!
Deep Space Industries,行星采矿公司,与卢森堡政府签署合作备忘录合作研发和建造DSI第一艘宇宙飞船。该宇宙飞船命名为Prospector-X,将测试近地轨道关键技术,此类技术是未来
点滴科技资讯
2018/04/28
2.9K0
这家公司要到太空采矿了!!
七夕要到了,用Python比心表白
每到各种节日,不少小伙伴们都会遇到这样一个世纪问题——怎么给心仪的女生/女朋友/老婆一个与众不同的节日惊喜。
Crossin先生
2021/08/23
1.2K0
七夕要到了,用Python比心表白
XLNet团队:公平对比,BERT才会知道差距!
我们认为使用大型模型架构和相同数据在XLNet 和BERT之间进行公平的比较研究具有重要的科学价值。
AI科技评论
2019/07/30
5780
XLNet团队:公平对比,BERT才会知道差距!
有具体问题,才会有好答案
今天文章的主题是,有好问题,才会有好答案。为什么会有这个想法,其实灵感来源于上周日和飞巴连麦时,一位开发小姐姐的提问。
程序媛淼淼
2022/09/01
1980
你以为只有迪士尼才会讲好故事?
机器之心原创 作者:吴昕 不是只有迪士尼才会讲好故事。 戴上 HTC Vive 头盔,打开宜家 AR App,你就可以像熊孩子一样打开抽屉,取出锅子放到灶台上,或者更换橱柜的颜色、材质,随心所欲设计厨房。 选择成人或孩子高度视角 六年前,宜家还推过一款 App,用户只需将宜家购物手册放到希望摆放某款家具的位置,从手机屏幕上看,被选中的虚拟家具已经被投射到这个位置。你可以设计可视化客厅,甚至精确到毫米。 “虚拟现实发展迅猛,在 5 到 10 年内,会影响到人们生活的方方面面。在未来,虚拟现实将在顾客群中
机器之心
2022/03/04
3080
iOS开发UINavigation系列四——导航控制器UINavigationController
        在前面的博客中,我么你介绍了UINavigationBar,UINavigationItem和UIToolBar,UINavigationController是将这些控件和UIViewController紧密的结合了起来,使用导航,我们的应用程序层次会更加分明,对controller的管理也更加方便。前几篇博客地址如下:
珲少
2018/08/15
1.8K0
iOS开发UINavigation系列四——导航控制器UINavigationController
4G手机有多久才会被淘汰?
虽然现在的5G技术已经非常流行,但从技术升级的角度是向前兼容,就是在假设5G设备的同时还要能够兼容2G,3G,4G的技术,这样就不用担心自己的4G手机是不是不能使用了,如果因为运营商的问题上线5G了,4G手机都不能使用了这是普通的用户所不能够接受的,所以现在不用担心自己使用4G手机在5G上线的时候不能够使用了,只不过享受不了5G的服务服务而已,整体来讲没有什么影响。
程序员互动联盟
2020/04/03
7270
「WordPress」Swift Performance V2.3.6.6 已激活汉化版|WordPress 优化插件
Swift性能插件的首要特点是提高 WordPress网页的运行效率,其性能与 WP快速缓存、W3高速缓存均是 WP Rocket的有力竞争者。它的特点很多,包括 HTML, CSS, JavaScript,图片和数据库的最优。
小伍同学
2022/12/20
5030
「WordPress」Swift Performance V2.3.6.6 已激活汉化版|WordPress 优化插件
Spring Bean 生命周期之“我要到哪里去”?
上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P
用户4172423
2019/09/04
6040
Spring Bean 生命周期之“我要到哪里去”?
只有程序员才会玩的游戏
这是一个在线解谜游戏,画风简洁,部分关卡需要具备一定的web知识。url地址直接访问,打开浏览器调试等操作都是解谜必要的。对于web开发人员来说非常值得一玩。
编程珠玑
2019/07/12
6350
只有程序员才会玩的游戏

相似问题

Android AlertDialog要到bluetoothsocket.connect()之后才会显示。

27

启动应用程序要到断电时才会启动

22

翻转DetailsViewController

23

何时发布detailsViewController

38

至少等待5分钟,元素才会出现

22
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文