Ios 以编程方式为我的导航栏创建了标题,但是,标题是否显示在正确的位置?

Ios 以编程方式为我的导航栏创建了标题,但是,标题是否显示在正确的位置?,ios,swift,uinavigationbar,Ios,Swift,Uinavigationbar,嗨 如果有人能帮我解决以下问题,我将不胜感激 我创建了一个UINavigationBar类,我在ViewController中创建了一个实例,我想在其中实现它。然而,导航栏的标题并没有显示在我想要的地方。如图所示,标题当前显示在导航栏的最顶端(即深蓝色区域)。如何使标题显示在浅蓝色区域中?如何根据需要修改其在导航栏区域内的位置 谢谢, 沙迪 嗯,我设法解决了我的问题,但是,任何进一步的选择都是非常受欢迎的 基本上,我在UINavigationBar类的导航栏中添加了一个UILabel,并设法解决

如果有人能帮我解决以下问题,我将不胜感激

我创建了一个
UINavigationBar
类,我在
ViewController
中创建了一个实例,我想在其中实现它。然而,导航栏的标题并没有显示在我想要的地方。如图所示,标题当前显示在导航栏的最顶端(即深蓝色区域)。如何使标题显示在浅蓝色区域中?如何根据需要修改其在导航栏区域内的位置

谢谢, 沙迪


嗯,我设法解决了我的问题,但是,任何进一步的选择都是非常受欢迎的

基本上,我在UINavigationBar类的导航栏中添加了一个UILabel,并设法解决了这个问题

有关解决方案,请参阅以下代码:


嗯,我设法解决了我的问题,但是,任何进一步的选择都是非常受欢迎的

基本上,我在UINavigationBar类的导航栏中添加了一个UILabel,并设法解决了这个问题

有关解决方案,请参阅以下代码:

import UIKit
import ChameleonFramework

class CustomNavigationBar: UINavigationBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupNavigationBar()  
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupNavigationBar()
    }

    func setupNavigationBar() {
        isTranslucent = true
        backgroundColor = .blue
        barStyle = .blackTranslucent
        var fsafdsaf = topItem?.title
        fsafdsaf = "dsadfsadfsafsdafsad"
        let titlfsdfe = UINavigationItem.init(title: fsafdsaf!)
        setItems([titlfsdfe], animated: false)
    }

}
import UIKit

import ChameleonFramework

class CustomNavigationBar: UINavigationBar {

    override init(frame: CGRect) {

        super.init(frame: frame)

        setupNavigationBar()

    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        setupNavigationBar()

    }

    func setupNavigationBar() {

        isTranslucent = true

        backgroundColor = .blue

        barStyle = .blackTranslucent

        let navigationBarTitleFrame = CGRect(x: 200, y: 50, width: 120, height: 25)

        let navigationBarTitle = UILabel(frame: navigationBarTitleFrame)

        navigationBarTitle.text = "This is my title"

        navigationBarTitle.textAlignment = .center

        navigationBarTitle.textColor = .white

        navigationBarTitle.layer.borderWidth = 3.0

        self.addSubview(navigationBarTitle)

    }

}