简体   繁体   English

如何在中间的导航栏中居中显示大标题?

[英]How to center a large title in navigation bar in the middle?

I want an app whose navigation bar looks like this at the end:我想要一个导航栏最后看起来像这样的应用程序:

图片

At the moment I'm failing to center the large title of the navigation bar in the middle and next to the back button.目前,我无法将导航栏的大标题居中放置在后退按钮的中间和旁边。

Can someone please help me with that?有人可以帮我吗? I don't have any code for this problem yet and I found no solutions on the internet.我还没有解决这个问题的任何代码,我在互联网上也没有找到解决方案。 :/ :/

I know that this is very late but I had the same issue and found couple of ways to achieve it.我知道这已经很晚了,但我遇到了同样的问题,并找到了几种方法来实现它。 Please mark the answer as correct if it works.如果有效,请将答案标记为正确。 These methods have been tested and works on Xcode 12.4 IOS 13这些方法已经过测试,适用于 Xcode 12.4 IOS 13


1. When you want to have a custom offset: 1. 当您想要自定义偏移量时:

extension UINavigationBar{
    @IBInspectable var LargeTitleOffSet: CGFloat{
        get{
            let pstyle =  largeTitleTextAttributes?[NSAttributedString.Key.paragraphStyle] as? NSParagraphStyle
            return pstyle?.firstLineHeadIndent ?? 0
        }
        set{
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.firstLineHeadIndent = newValue
            let attributes: [NSAttributedString.Key: NSParagraphStyle] = [.paragraphStyle: paragraphStyle]
            self.largeTitleTextAttributes = attributes
        }
    }
}

And then go to the storyboard and select your NavigationController's NavigationBar then from the Attributes Inspector set your offset as shown in the image below.然后转到故事板并选择您的NavigationController 的 NavigationBar,然后从 Attributes Inspector 中设置您的偏移量,如下图所示。 Note: you might have to clean the build folder and build and run every-time you change the offset for the changes to show up.注意:每次更改偏移量时,您可能都必须清理构建文件夹并构建和运行以显示更改。 在此处输入图片说明


2. When you want the title to be exactly in the centre and not be able to change it from storyboard 2. 当您希望标题正好位于中心并且无法从故事板更改时

add the following code to one of your classes:将以下代码添加到您的类之一:

extension UIViewController{
    func centerTitle(){
        for navItem in(self.navigationController?.navigationBar.subviews)! {
             for itemSubView in navItem.subviews {
                 if let largeLabel = itemSubView as? UILabel {
                    largeLabel.center = CGPoint(x: navItem.bounds.width/2, y: navItem.bounds.height/2)
                    return;
                 }
             }
        }
    }
}

And call it from ViewDidAppear (not ViewDidLoad) :并从ViewDidAppear(不是 ViewDidLoad)调用它:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.centerTitle() //Only this VC's title will be centered
}

Below is the screenshot of the desired output:以下是所需输出的屏幕截图: 在此处输入图片说明

If you have a navigation controller you can place this line of code inside of the pushed view controller "viewDidLoad" method and it should auto center the titleView for you:如果你有一个导航控制器,你可以将这行代码放在推送的视图控制器“viewDidLoad”方法中,它应该为你自动将 titleView 居中:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Albums"
}

You don't have to center anything, you just have to embed your UIViewController in a UINavigationController .你不必居中任何东西,你只需要将你的UIViewController嵌入到UINavigationController

Follow this:按照这个:

Go to your AppDelegate and make it look like this:转到您的 AppDelegate 并使其看起来像这样:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let yourViewController = ViewController()
    yourViewController.title = "Albums"
    window?.rootViewController = UINavigationController(rootViewController: yourViewController)
    window?.makeKeyAndVisible()

    return true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM