简体   繁体   English

导航栏titleView对齐

[英]Navigation bar titleView alignment

I want my navigation bar to show two things in the middle. 我希望我的导航栏在中间显示两个东西。 One them is going to be List name, and the other one will be name of users. 其中一个是List名称,另一个是用户名。 User names will be placed under list name. 用户名将放在列表名称下。 What i have done so far is, i created two label and one superview programatically and set the titleView of navigationItem . 我做了什么至今,我创建了两个标签,一个上海华编程和设置titleViewnavigationItem

override func viewDidLoad() {
        super.viewDidLoad()
        var rectForView = CGRect(x: 0, y: 0, width: 190, height: 176)
        var viewForTitle = UIView(frame: rectForView)

        var rectForNameLabel = CGRect(x: 0, y: 63, width: 190, height: 30)
        var listNameLabel = UILabel(frame: rectForNameLabel)
        listNameLabel.text = "Name of the List...."
        listNameLabel.textAlignment = NSTextAlignment.Center
        listNameLabel.font = UIFont(name: "HelveticaNeue-Bold", size: 15)

        var rectForListLabel = CGRect(x: 0, y: 93, width: 190, height: 20)
        var usersOfListLabel = UILabel(frame: rectForListLabel)
        usersOfListLabel.text = "some Users...."
        usersOfListLabel.textAlignment = NSTextAlignment.Center
        usersOfListLabel.font = UIFont(name: "HelveticaNeue", size: 10)

        viewForTitle.addSubview(listNameLabel)
        viewForTitle.addSubview(usersOfListLabel)


        self.navigationItem.titleView = viewForTitle

The thing is, I'm picking width randomly, which leads to alignment problems in different devices. 问题是,我随机选取宽度,这会导致不同设备的对齐问题。 How to achieve middle alignment in my case? 在我的案例中如何实现中间对齐? iPhone 6

iphone 5

The thing is, navigation titleView always try to center your view. 问题是,导航titleView总是试图使您的视图居中。 The reason that the title you see off-center in a smaller screen is that, the width of your custom titleView has gone beyond the maxmium width of titleView. 您在较小的屏幕中看到偏离中心的标题的原因是,自定义titleView的宽度超出了titleView的最大宽度。

If you set the value to 160, you will see it centered for the second situation. 如果将值设置为160,您将看到它以第二种情况为中心。

General Solution 一般解决方案

If you want to adapt title width to different devices, there are a few approaches that may help you solve the problem. 如果要将标题宽度调整为不同的设备,可以使用一些方法来帮助您解决问题。

  1. Set different width based on different screen width. 根据不同的屏幕宽度设置不同的宽度。 For example, when screen width is 320, set it no more than 160. If larger than 320, assign value based on different screen size. 例如,当屏幕宽度为320时,将其设置为不超过160.如果大于320,则根据不同的屏幕大小指定值。 This is not an elegant solution, but pretty much straightforward without banging your head into setting and trying complicated constraints. 这不是一个优雅的解决方案,但是在没有敲打设置和尝试复杂约束的情况下非常直截了当。

  2. Calculate the actual width of titleView. 计算titleView的实际宽度。 The width of titleView is determined by navigation bar width minus left and right barbuttonitem. titleView的宽度由导航栏宽度减去左右barbuttonitem确定。 Something like 就像是

TitleViewWidth = Navigation width - leftbarbuttonitem.width +/- rightbarbuttonitem.width - someConstant TitleViewWidth =导航宽度 - leftbarbuttonitem.width +/- rightbarbuttonitem.width - someConstant

You need a little bit experiment for finding the appropriate constant that fits this formula. 您需要进行一些实验,以找到适合此公式的适当常量。

  1. Create a xib file for this titleView, set up constraints more flexibly than claim a fixed width. 为此titleView创建一个xib文件,设置约束比声明固定宽度更灵活。 Load view from xib and then assign to titleView. 从xib加载视图,然后分配给titleView。 This may cost longer time to tune it to work. 这可能需要更长的时间来调整它的工作。

Edit 编辑

I think of another way that may be easier for you if you are new to this. 如果你是新手,我想到另一种可能对你更容易的方式。

In the storyboard, find and drop a view to this view controller's navigation bar titleView area. 在故事板中,查找并删除视图到此视图控制器的导航栏titleView区域。

Expand however long you want the view to be. 无论你希望视图如何,都要展开。

Drop two labels to that view, and set the leading and trailing and height and vertical constraints. 将两个标签拖放到该视图,并设置前导和尾随以及高度和垂直约束。

How it looks on storyboard 它如何在故事板上看起来

故事板图像

How it runs on 4S 它是如何在4S上运行的

4S跑

Approach: 做法:

  • titleView is always centred titleView始终居中
  • So set your custom view's width to match that of the navigationBar (width -40) 因此,设置自定义视图的宽度以匹配navigationBar宽度(宽度-40)

Code

private func setupNavigationItems() {

    let label = UILabel()

    label.translatesAutoresizingMaskIntoConstraints = false

    label.text = "aaaaa"
    label.backgroundColor = .green

    label.textAlignment = .left

    navigationItem.titleView = label

    if let navigationBar = navigationController?.navigationBar {

        label.widthAnchor.constraint(equalTo: navigationBar.widthAnchor, constant: -40).isActive = true
    }
}

自定义标题视图,用于导航栏的整个宽度

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

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