【问题标题】:How to create a custom navigation bar in Swift Xcode?如何在 Swift Xcode 中创建自定义导航栏?
【发布时间】:2017-10-31 10:20:48
【问题描述】:

我正在为我的 iOS 毕业项目开发一个聊天应用程序。我设计了如下导航栏:

现在我正在尝试在我的 Xcode 项目中开发上述图形,但我不知道这是否是实现此目的的正确方法,并且不像图形那样得到它。

我正在使用一个 xib 文件,我使用它的一个实例将其加载到我的 ViewController.swift 中。不如将其添加为navigationItemtitleView

let helperView = HelperView()
navigationItem.titleView = helperView

这是上面代码sn-p的结果:

这个结果的问题是它与左栏按钮项目重叠,另一个我仍然没有弄清楚的问题是,如果消息气泡可以有动态高度,当它有多行时(最大值为3 行)。

有没有人在 Xcode 中使用过这种设计,这是正确的方法,还是有更好的方法来实现。也许是一个自定义 UINavigationController 类?

【问题讨论】:

    标签: ios swift xcode uinavigationcontroller


    【解决方案1】:

    尝试创建您想要的整个导航视图,即宽度等于视图,然后尝试使用此代码添加它

        navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationController?.navigationBar.shadowImage = UIImage()
        navigationController?.navigationBar.backgroundColor = .clear
    
        navigationController?.view.insertSubview(subview, belowSubview: navigationController?.navigationBar)
    

    它会让你的导航栏变得不可见,但仍然显示栏按钮

    更新,来自 dimpiax

    但最好覆盖您的 UINavigationController 类,并在 viewDidLoad 中设置视图

    navigationBar.shadowImage = UIImage()
    navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationBar.backgroundColor = .clear
    

    并依赖于 viewController 的视图 - 显示特定视图。

    【讨论】:

    • 所以如果我理解正确的话。我应该在我的 Xib 文件中正确创建头像和消息气泡,而不是在 viewDidAppear 函数中添加上述代码?子视图应该是什么?
    • 子视图是您要添加的视图,在本例中是您的helperView。您可以在viewDidLoad 中添加上述代码,但将指针变量保留到您的helperView 以调整viewDidLayoutSubview 中的框架大小
    • 好的,关于指针变量的最后一部分是什么意思?你的意思是它的框架应该根据它的内容调整大小吗?
    • 我的意思是,如果您在viewDidAppear 中添加此代码,它可能会出现闪烁,添加viewDidLoad 那么它可能有错误的框架,这就是您可能想要声明@ 的原因987654331@ 在您的视图控制器范围内更改其框架在 viewDidLayoutSubview
    • 那么添加上述代码的最佳方式是在 de viewdidLayoutSubview 函数中?对不起,如果我理解错了。
    【解决方案2】:
    // --------------------------------------------------------
    // MARK:- Navigation SetUp
    // --------------------------------------------------------
    
    private func _navigationBarSetUp(){
        
        ///# Navigatoin bar and title #///
        
        navigationController?.navigationBar.isHidden = false
        navigationItem.title = vEditScreenName
        navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : ColorTheme.white, NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "Poppins-Regular", size: 20), size: 20)]
        self.view.backgroundColor = ColorTheme.vidBoomBrandPurple
        
        ///# Navigation bar back button #///
        
        let btnBack = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
        btnBack.setImage(UIImage(named: "icon_back_arrow"), for: .normal)
        btnBack.addTarget(self, action: #selector(_handlebtnBackTapped), for: .touchUpInside)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btnBack)
        
        ///# Navigation bar explore button #///
        
        let btnExport = UIButton(frame: CGRect(x: 0, y: 0, width: 80, height: 25))
        btnExport.setTitle("Export", for: .normal)
        btnExport.backgroundColor = .orange
        btnExport.layer.cornerRadius = 18
        btnExport.addTarget(self, action: #selector(_handleBtnExportTapped), for: .touchUpInside)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btnExport)
    }
    
    
    // --------------------------------------------------------
    // MARK:- UIBarButtonItem Action
    // --------------------------------------------------------
    
    @objc fileprivate func _handlebtnBackTapped() {
        self.player.stop()
        navigationController?.popViewController(animated: true)
        navigationController?.navigationBar.isHidden = true
    }
    
    @objc func _handleBtnExportTapped(){
        self.player.stop()
        let svc = ShareVideoVC(nibName: "ShareVideoVC", bundle: nil)
        svc.videoString = videoString
        saveFile()
        self.navigationController?.pushViewController(svc, animated: true)
    }
    

    【讨论】:

    • 你能补充一些关于如何使用这个代码的细节吗?这些方法应该放在哪里,我们是否需要从某个位置/地方手动调用它们?
    • 将此代码添加到 ViewController 类但不在 viewdidload 中并调用 viewdidload 中的函数。
    猜你喜欢
    • 2012-12-12
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多