如何提高导航栏主标题的位置?

时间:2019-04-18 14:27:17

标签: ios swift

我已经在互联网上进行了彻底搜索,无法找到如何调整navigationItem.title出现的高度。我指的是显示在导航栏中间的标题。

2 个答案:

答案 0 :(得分:1)

使用setTitleVerticalPositionAdjustment的{​​{1}}功能,可以为导航栏标题设置所需的垂直偏移(可以为正或负)。进一步了解here


 祝你好运:)

答案 1 :(得分:0)

Tomas ”答案是最简单的答案。除此之外,您可以使用自定义titleView。这样,您可以水平和垂直插入文本:

class TitleView: UIView {

    private let label: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 15)
        return label
    }()

    var title: String? {
        get { return label.text }
        set { label.text = newValue }
    }

    convenience init(title: String, inset: UIEdgeInsets) {
        self.init()

        label.text = title

        addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.topAnchor.constraint(equalTo: topAnchor, constant: inset.top).isActive = true
        label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: inset.left).isActive = true
        bottomAnchor.constraint(equalTo: label.bottomAnchor, constant: inset.bottom).isActive = true
        trailingAnchor.constraint(equalTo: label.trailingAnchor, constant: inset.right).isActive = true
    }

}

然后您可以像这样简单地使用它:

let titleView = TitleView(title: "TitleView", inset: .init(top: -10, left: 0, bottom: 0, right: 0))
vc.navigationItem.titleView = titleView