【问题标题】:UIBarButtonItem size displays wrongUIBarButtonItem 大小显示错误
【发布时间】:2018-07-29 18:13:29
【问题描述】:

我有一些自定义的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)
}

但是我的 BackButton 看起来不对。 按钮高度有什么问题?

【问题讨论】:

    标签: ios swift uinavigationbar uibarbuttonitem uinavigationitem


    【解决方案1】:

    如果条形按钮项具有自定义视图,您必须使用内部自动布局约束来调整自定义视图的宽度。

    但是你不能像你正在做的那样将高度设置为大于运行时喜欢的大小。删除这些行:

    let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78)
    currHeight?.isActive = true
    

    【讨论】:

    • 但我有:let currWidth = menuBarItem.customView?.widthAnchor.constraint(equalToConstant: 78) currWidth?.isActive = true let currHeight = menuBarItem.customView?.heightAnchor.constraint(equalToConstant: 78) currHeight?.isActive = true
    • 而且您在 Xcode 控制台中也没有收到投诉?
    • 我删除了 constraint(equalToConstant: 78) 并且没有任何变化。我没有收到来自 Xcode 控制台的错误和投诉
    • 我删除了menuBtn.frame = CGRect(x: 0.0, y: 0.0, width: 78, height: 78) 并添加了constraint(equalToConstant: 78) 得到一些控制台警告:"<NSLayoutConstraint:0x600002a48a50 UILayoutGuide:0x6000030ad340'LeadingBarGuide(0x7ff5ef53dd00)'.height == 44 (active)>", "<NSLayoutConstraint:0x600002a66d00 _UIButtonBarStackView:0x7ff5ef488890.top == UILayoutGuide:0x6000030ad340'LeadingBarGuide(0x7ff5ef53dd00)'.top (active)>", "<NSLayoutConstraint:0x600002a4a8a0 UIButton:0x7ff5ef59bab0.height == 78 (active)>"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多