1

是否可以使用 swift 以编程方式将 UIStackView 放置在 NavigationBar 中?我想在 StackView 中放置两个排列好的 stackview。但是当我这样做时,它在导航栏中没有显示任何内容。如果可能,请提供示例。谢谢

4

3 回答 3

2

最后我找到了解决方案

let btnSort   = UIButton(type: .system)
    btnSort.frame =  CGRect(x: 0, y: 0, width: 120, height: 40)
    btnSort.tintColor = UIColor.white
    btnSort.setImage(UIImage(named:"ic_controls_icon.png"), for: .normal)
    btnSort.imageEdgeInsets = UIEdgeInsets(top: 6,left: -10,bottom: 6,right: 34)
    btnSort.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 14)
    btnSort.setTitle("SORT", for: .normal)
    btnSort.layer.borderWidth = 1.0
    btnSort.backgroundColor = UIColor.red //--> set the background color and check
    btnSort.layer.borderColor = UIColor.white.cgColor

    let btnControl   = UIButton(type: .system)
    btnControl.frame =  CGRect(x: 0, y: 0, width: 120, height: 40)
    btnControl.tintColor = UIColor.white
    btnControl.setImage(UIImage(named:"ic_controls_icon.png"), for: .normal)
    btnControl.imageEdgeInsets = UIEdgeInsets(top: 6,left: -10,bottom: 6,right: 34)
    btnControl.titleEdgeInsets = UIEdgeInsets(top: 0,left: 0,bottom: 0,right: 14)
    btnControl.setTitle("SORT", for: .normal)
    btnControl.layer.borderWidth = 1.0
    btnControl.backgroundColor = UIColor.red //--> set the background color and check
    btnControl.layer.borderColor = UIColor.white.cgColor

    let view = UIStackView(frame: CGRect(x: 0, y: 0, width: 300, height: 50))
    view.axis = .horizontal
    view.distribution = .fillEqually
    view.spacing = 5
    view.addArrangedSubview(btnSort)
    view.addArrangedSubview(btnControl)

    let mainTitleView = UIView(frame: CGRect(x: 0, y: 0, width: 300, height: 50))
    mainTitleView.addSubview(view)


    navigationItem.titleView = mainTitleView
于 2018-04-24T13:49:26.920 回答
2

您可以使用视图控制器的属性将任何子UIView类(其中UIStackView之一)设置为导航栏的标题。navigationItem.titleView

你可以在操场上测试一下……</p>

import UIKit
import PlaygroundSupport

let vc = UIViewController()
vc.view.backgroundColor = .white
let nav = UINavigationController(rootViewController: vc)

let topLabel = UILabel()
topLabel.font = UIFont.boldSystemFont(ofSize: 20)
topLabel.text = "Hello"

let bottomLabel = UILabel()
bottomLabel.font = UIFont.systemFont(ofSize: 16)
bottomLabel.text = "World!"

let stackView = UIStackView(arrangedSubviews: [topLabel, bottomLabel])
stackView.axis = .vertical

vc.navigationItem.titleView = stackView

PlaygroundPage.current.liveView = nav.view
PlaygroundPage.current.needsIndefiniteExecution = true

在此处输入图像描述

于 2018-04-24T14:20:25.810 回答
0
var navTitle: String? = "Preview Checklist"
var navSubTitle: String? = "Edit Checklist >"

  lazy var titleStackView: UIStackView = {
    
    let titleLabel = UILabel()
    titleLabel.textAlignment = .left
    titleLabel.text = navTitle
  //titleLabel.font = UIFont(name: "RawlineMedium-Regular", size:CGFloat(15))
    titleLabel.textColor = .white
    
    
    let subtitleLabel = UILabel()
    subtitleLabel.textAlignment = .left
    subtitleLabel.text = navSubTitle
  //subtitleLabel.font = UIFont(name: "RawlineMedium-Regular", size:CGFloat(11))
    subtitleLabel.textColor = .white
    
    let stackView = UIStackView(arrangedSubviews: [ titleLabel, subtitleLabel])
    stackView.axis = .vertical
    stackView.backgroundColor = .blue
    
    return stackView
}()

  override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.titleView = titleStackView
  }
于 2020-08-17T19:51:47.677 回答