大标题,中心对齐

时间:2018-02-11 06:46:49

标签: ios swift uinavigationbar large-title

我尝试将标题置于大标题模式中心,但该代码并不会影响。在AppDelegate中:

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

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.paragraphStyle: paragraphStyle]

    return true
}

3 个答案:

答案 0 :(得分:1)

大标题不能居中,它的位置在左侧并且左对齐。标题的大小无关紧要,如果您希望标题居中,则必须创建自己的自定义标题。

答案 1 :(得分:0)

我通过将navigationItem标题设置为none来解决此问题。然后使用我的文本设置自定义标签,并以编程方式向标签添加约束。这是下面的代码。

func ExampleWriter_WriteAll() {
    records := [][]string{
        {"first_name", "last_name", "username"},
        {"Rob", "Pike", "rob"},
        {"Ken", "Thompson", "ken"},
        {"Robert", "Griesemer", "gri"},
    }

    w := csv.NewWriter(os.Stdout)
    w.WriteAll(records) // calls Flush internally

    if err := w.Error(); err != nil {
        log.Fatalln("error writing csv:", err)
    }
    // Output:
    // first_name,last_name,username
    // Rob,Pike,rob
    // Ken,Thompson,ken
    // Robert,Griesemer,gri
}

答案 2 :(得分:-1)

只要有足够的空间,

UINavigationBar 就会自动将其titleView居中。我想你的代码不会影响实际的titleView框架,因为你没有后退按钮或rightBarButton,所以 UINavigationBar 会自动将titleView延伸到右边。

要修复它,您可以添加空的右栏按钮,或添加带有预定义框架的自定义titleView