添加左键和标题后,右键不显示

时间:2019-06-08 11:38:58

标签: swift uinavigationbar

我想设计下图所示的导航视图 enter image description here

我正在添加此代码

  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]

      } 

enter image description here

1 个答案:

答案 0 :(得分:0)

“ ic_logo”图片大小是多少?

我要这个是因为我尝试了多个图像作为导航栏按钮和:

enter image description here

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

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

尝试更改“ ic_logo”的大小。