【问题标题】:Right button not showing after add left button and title添加左键和标题后右键不显示
【发布时间】:2019-08-07 16:19:29
【问题描述】:

我想设计如下图的导航视图:

我正在添加此代码

func setupUpHomePageNavigation(title:String, subtitle:String) {

    let back_button = UIBarButtonItem(image: #imageLiteral(resourceName: "ic_logo"), style: .plain, target: self, action: #selector(self.btnKbImageClicked))

    self.navigationItem.leftBarButtonItems = [back_button]

    let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    space.width = 0.0

    let one = UILabel()
    one.text = title
    one.textAlignment = .left
    one.textColor = UIColor.black
    one.backgroundColor = .green
    one.font = UIFont.systemFont(ofSize: 15)
    one.sizeToFit()

    let two = UILabel()
    two.text = subtitle
    two.textAlignment = .left
    two.backgroundColor = .yellow
    two.textColor = .darkGray
    two.font = UIFont.systemFont(ofSize: 12)
    two.textAlignment = .center
    two.sizeToFit()


    let stackView = UIStackView(arrangedSubviews: [one, two])
    stackView.distribution = .equalCentering
    stackView.axis = .vertical
    stackView.alignment = .leading
    stackView.backgroundColor = .red

    one.sizeToFit()
    two.sizeToFit()

    stackView.translatesAutoresizingMaskIntoConstraints = false
    stackView.widthAnchor.constraint(equalToConstant:150).isActive = true

    //self.navigationItem.titleView = titleView

    self.navigationItem.leftBarButtonItems = [back_button,space,UIBarButtonItem(customView: stackView)]

    let account_button = UIBarButtonItem(image:#imageLiteral(resourceName: "user-silhouette"), style: .plain, target: self, action: #selector(self.btnAccountClicked))
            account_button.tintColor = .black

    let search_button = UIBarButtonItem(image:#imageLiteral(resourceName: "search (2)"), style: .plain, target: self, action: #selector(self.btnSearchClicked))
    search_button.tintColor = .black


    self.navigationItem.rightBarButtonItems = [account_button,search_button]

} 

【问题讨论】:

    标签: swift uinavigationbar


    【解决方案1】:

    “ic_logo”图片尺寸是多少?

    我要求这样做是因为我为导航栏按钮尝试了多个图像并且:

    使用最小 90*90 尺寸的图像,然后是 2x 和 3x,我得到了高于结果。我正在使用与您相同的代码。只是我正在更改图像大小。

    如果我进一步增加尺寸,空间将会增加。理想情况下,如果您想将左侧栏按钮图标保留在左侧,则图像尺寸需要为 30*30 或 40*40 并且是倍数。然后用户界面将是完美的。

    尝试更改“ic_logo”的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 2017-02-02
      相关资源
      最近更新 更多