我有一些自定义UINavigationBar
(用于JSQMessagesViewController)。但是我对leftBarButtonItem
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 28, width: screenSize.width, height: 78))
let navItem = UINavigationItem(title: chatName ?? "chat")
let menuBtn = UIButton(type: .custom)
menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 78, height: 78)
menuBtn.setImage(UIImage(named: "backButton"), for: .normal)
menuBtn.addTarget(self, action: #selector(back), for: UIControl.Event.touchUpInside)
let menuBarItem = UIBarButtonItem(customView: menuBtn)
let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 78)
currWidth?.isActive = true
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
currHeight?.isActive = true
navItem.leftBarButtonItem = menuBarItem
navBar.titleTextAttributes = [.font : UIFont(name: "Acrom-Bold", size: 26)!, .foregroundColor : UIColor.white]
navBar.setItems([navItem], animated: false)
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.backgroundColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 0.3)
navBar.isTranslucent = true
self.view.addSubview(navBar)
}
答案 0 :(得分:0)
如果条形按钮项具有自定义视图,则必须使用内部自动布局约束来调整自定义视图的宽度。
但是,您不能像执行操作那样将高度设置为大于运行时所喜欢的高度。删除这些行:
let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
currHeight?.isActive = true