swift - 我正在尝试创建一个 UIbarButtonItem,其宽度和高度等于导航栏高度的 70%?

标签 swift

我正在尝试编写 UIBarButtonItem 的扩展。我想使用自动布局,以便 UIBarButtonItem 的宽度和高度为 UINavigationBar 高度的 70%。当我实现此扩展时,我收到以下自动布局错误:

import UIKit

extension UIBarButtonItem {

    static func menuButton(target: Any?, action: Selector, imageName: String, navigationBar: UINavigationBar) -> UIBarButtonItem{

        let button = UIButton.init(type: .system)
        let image = UIImage.init(named: imageName)
        button.setBackgroundImage(image, for: .normal)
        button.addTarget(target, action: action, for: .touchUpInside)

        let menuButton = UIBarButtonItem.init(customView: button)
        menuButton.customView?.translatesAutoresizingMaskIntoConstraints = false
        menuButton.customView?.widthAnchor.constraint(equalTo: navigationBar.heightAnchor, multiplier: 0.7).isActive = true
        menuButton.customView?.heightAnchor.constraint(equalTo: navigationBar.heightAnchor, multiplier: 0.7).isActive = true

        return menuButton
    } 
}

实现:

let rightButton = UIBarButtonItem.menuButton(target: self, action: #selector(editCells), imageName: "expand", navigationBar: navigationController!.navigationBar)

navigationItem.rightBarButtonItem = rightButton

控制台中的错误消息:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors <NSLayoutDimension:0x600001be1180 "UIButton:0x7ff5d6d16b80.width"> and <NSLayoutDimension:0x600001be25c0 "UINavigationBar:0x7ff5d911a2f0.height"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

最佳答案

您可以为导航栏自定义按钮创建自定义 View ,并为您想要的任何内容设置属性,如下所示:

func addRightButton() {

    let barButtonView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 180, height: 40)))
    barButtonView.backgroundColor = UIColor.red
    let navigationBarHeightWithRatio = (navigationController?.navigationBar.frame.height ?? 100) * 0.7
    let customBarButton = UIButton(frame: CGRect(x: 0, y: 8, width: navigationBarHeightWithRatio, height: navigationBarHeightWithRatio))
    customBarButton.setImage(UIImage(named: "your_image_name"), for: .normal)
    customBarButton.setTitle("title", for: .normal)

    customBarButton.addTarget(self, action: #selector("<your_action_function>"), for: .touchUpInside)

    barButtonView.addSubview(customBarButton)

    let rightBarButton = UIBarButtonItem(customView: barButtonView)
    navigationItem.rightBarButtonItem = rightBarButton
}

注意:

  • 您可以自定义宽度、不同的宽度、高度值以及 x、y 位置。

我希望它能起作用。

享受吧。

关于swift - 我正在尝试创建一个 UIbarButtonItem,其宽度和高度等于导航栏高度的 70%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57149137/

相关文章:

ios - 在不缩小内容的情况下调整 Sprite 大小

ios - 我假设我的程序执行无序,因为某些线程先于另一个线程完成。 NSLock,GDC,有什么建议吗?

ios - 在 Swift playground 中禁用某些代码行的运行

ios - 点击按钮时分层

ios - ITMS-90476 - 无效 bundle (LaunchScreen 错误)

ios - 更新xcode后出现很多错误

ios - 从音乐库检索音乐,无法弄清楚如何从 MPMediaItem 检索歌曲的艺术图片和名称

ios - 使用 Swift 连接方法修改倒数第二个连接器?

swift - HKObjectQuery 不会停止

ios - 协议(protocol)方法未调用