Ios 导航栏标题视图上的多行UILabel

Ios 导航栏标题视图上的多行UILabel,ios,swift,uiview,uilabel,Ios,Swift,Uiview,Uilabel,我有一个自定义UIView,它有两个UILabel(相同宽度)和一个UIImageView(已知宽度为44pt)。宽度大小是作为一个例子给出的,它们可以改变,但确切地说,UILabels应该具有相同的宽度,而UIImage具有44点的宽度。我想将此视图添加到UINavigationBar”titleView,但是ImageView应该位于导航栏的中心 (60 width) UILabel---UIImageView (44 width) ---UILabel (60 width) 我希望UIL

我有一个自定义UIView,它有两个
UILabel
(相同宽度)和一个
UIImageView
(已知宽度为44pt)。宽度大小是作为一个例子给出的,它们可以改变,但确切地说,
UILabels
应该具有相同的宽度,而UIImage具有44点的宽度。我想将此视图添加到
UINavigationBar
titleView
,但是
ImageView
应该位于导航栏的中心

(60 width) UILabel---UIImageView (44 width) ---UILabel (60 width)
我希望UILabels最多有两行,并且
adJustFontSizeToFitWidth
true。我给标题视图指定了特定的宽度和高度,但是标签有两行,但是它们的字体大小不会改变,即使它们不适合视图

如何添加标题视图:

 navigationItem.titleView = myTitleView
    let widthOfItem: CGFloat = 30.0
    let pading: CGFloat = 40
    let aWidth: CGFloat = (self.navigationController?.navigationBar.frame.width)! - CGFloat(1) * widthOfItem * 2.0 - pading

    myTitleView { (make) in
        make.width.equalTo(aWidth)
        make.height.equalTo(44)
    }
MyCustomView:

override func layoutSubviews() {
    super.layoutSubviews()
    let preferredWidth = (bounds.width / 2) - 56
    firstLabel.preferredMaxLayoutWidth = preferredWidth
    secondLabel.preferredMaxLayoutWidth = preferredWidth
}

private func setupViews() {

    addSubview(firstLabel)
    addSubview(myImageView)
    addSubview(secondLabel)

    firstLabel.font = .myFont(.bold, size: 36)
    firstLabel.adjustsFontSizeToFitWidth = true
    firstLabel.minimumScaleFactor = 0.5
    firstLabel.textColor = .textPrimary
    firstLabel.numberOfLines = 2
    firstLabel.lineBreakMode = .byWordWrapping
    firstLabel.textAlignment = .right

    myImageView.contentMode = .scaleAspectFit
    myImageView.clipsToBounds = true
    myImageView.layer.minificationFilter = .trilinear
    myImageView.layer.cornerRadius = currencyImageSize.height / 2

    secondLabel.font = . myFont(.bold, size: 36)
    secondLabel.translatesAutoresizingMaskIntoConstraints = false
    secondLabel.textColor = .textPrimary
    secondLabel.numberOfLines = 2
    secondLabel.adjustsFontSizeToFitWidth = true
    secondLabel.baselineAdjustment = .none
    secondLabel.minimumScaleFactor = 0.5
    secondLabel.lineBreakMode = .byWordWrapping
    secondLabel.textAlignment = .left

    firstLabel.snp.makeConstraints { (make) in
        make.leading.equalToSuperview()
        make.top.bottom.equalToSuperview()
        make.trailing.equalTo(myImageView.snp.leading).offset(-12)
    }

    myImageView.snp.makeConstraints { (make) in
        make.height.equalToSuperview()
        make.width.equalTo(myImageView.snp.height)
        make.centerX.equalToSuperview()
    }
   secondLabel.snp.makeConstraints { (make) in
        make.top.bottom.equalToSuperview()
        make.leading.equalTo(myImageView.snp.trailing).offset(12)
        make.trailing.equalToSuperview()
    }
}

也许这个代码可以用


class CustomNavClass: UIView {

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

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

    private func setup_view() {
        backgroundColor = .lightGray

        let image = UIImageView(image: .init())
        addSubview(image)

        image.translatesAutoresizingMaskIntoConstraints = false
        image.backgroundColor = .green

        NSLayoutConstraint.activate([
            image.centerXAnchor.constraint(equalTo: centerXAnchor),
            image.widthAnchor.constraint(equalToConstant: 44),
            image.heightAnchor.constraint(equalToConstant: 44),

            image.topAnchor.constraint(equalTo: topAnchor, constant: 3),
            image.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -3)
        ])

        let leftLabel = UILabel()
        leftLabel.text = "Label label left long label will go here, naturally"
        leftLabel.numberOfLines = 2
        leftLabel.adjustsFontSizeToFitWidth = true
        leftLabel.backgroundColor = .red

        addSubview(leftLabel)

        leftLabel.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            leftLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
            leftLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            leftLabel.trailingAnchor.constraint(equalTo: image.leadingAnchor, constant: -10)
        ])

        let rightLabel = UILabel()
        rightLabel.text = "Label label right long label will go here, naturally"
        rightLabel.numberOfLines = 2
        rightLabel.adjustsFontSizeToFitWidth = true
        rightLabel.backgroundColor = .blue

        addSubview(rightLabel)

        rightLabel.translatesAutoresizingMaskIntoConstraints = false

        NSLayoutConstraint.activate([
            rightLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
            rightLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            rightLabel.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 10)
        ])
    }
}
代码不使用
UIStackView
,因为其功能有限(具体而言,只能在
上展开一个
UIView
。填充
分布
属性

这将创建布局约束:

  • 固定在
    中心的
    UIImageView
    (具有3个顶部和底部约束,可选,您可以使用中心约束或类似约束)
  • 前缘
    UILabel
    通过
    UIImageView
    的前缘与前缘和后缘固定
  • 尾部
    UILabel
    通过
    UIImageView
    的后缘固定到后缘和前缘

  • 尝试将它们放在水平
    UIStackView
    中。问题是它们的大小不同。例如,图像的宽度为20,UILAbels的宽度为60,60。我可以在堆栈视图中这样做吗?是的,堆栈视图中的每个子视图都可以有不同的宽度。这不起作用。我无法得到我想要的。我编辑了我的问题在你的情况下,你只需要如果需要对两个标签的固定宽度进行约束,则stackview应考虑所有其他内容。请参见此处的示例:问题在于导航栏上有一个左栏按钮项,因此您可以向我演示在这种情况下如何将图像居中吗?左栏按钮项是否应显示在左标签旁边?我实际上是ex尽管制作左栏按钮项目的方法很粗糙,但它需要将视图从标题视图中删除,并放在导航栏的子视图中。是的。它在导航栏中,而不是标题视图中。这是一个常见的后退按钮,但当您在标题视图的左侧有一些项目时,您就会知道是total(屏幕宽度-条形按钮项),因此居中的内容是hackyOki!我将根据这些要求进行编辑。我测试了您的代码,当您使用默认字体设置文本时,它工作正常,但当我在创建视图后将字体大小设置为36PT粗体并设置文本时,它不工作。左标签甚至超出了标题视图的范围。