gpt4 book ai didi

ios - 无法设置 RightBarButtonItems 的位置

转载 作者:行者123 更新时间:2023-11-29 05:41:34 25 4
gpt4 key购买 nike

我想将背景图层的位置设置为如图所示的位置。

我尝试设置图层的位置,但不能。

func showRightNavBar()
{
let inboxButton = UIButton(type: .system)
inboxButton.setImage(UIImage (named: "email")?.withRenderingMode(.alwaysOriginal), for: .normal)
inboxButton.imageEdgeInsets = .init(top: 0, left: 35, bottom: 0, right: -30)

inboxButton.layer.backgroundColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)

inboxButton.addTarget(self, action: #selector(inboxButtonTapped), for: UIControl.Event.touchUpInside)

let qrButton = UIButton(type: .system)
qrButton.setImage(UIImage (named: "QR-code")?.withRenderingMode(.alwaysOriginal), for: .normal)

qrButton.imageEdgeInsets = .init(top: 5, left: 17, bottom: 5, right: -7)

qrButton.layer.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)

qrButton.addTarget(self, action: #selector(qrButtonTapped), for: UIControl.Event.touchUpInside)

let fixSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
fixSpace.width = 10

navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: qrButton),fixSpace,UIBarButtonItem(customView: inboxButton)]
}

我希望背景层位置会像图片中的位置一样。

enter image description here

最佳答案

我对您的代码进行了一些更改。

  • 按钮的图像大小应如下所示:

    90 X 90 for 3x
    60 X 60 for 2x
    40 X 40 for 1x
  • 检查行尾有注释的代码行。

    func showRightNavBar() {

    let inboxButton = UIButton(type: .system)
    inboxButton.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 40, height: 40)) // Set frame of button
    inboxButton.setImage(UIImage (named: "email")?.withRenderingMode(.alwaysOriginal), for: .normal)
    inboxButton.imageEdgeInsets = .init(top: 0, left: 25, bottom: 0, right: -30) // Minor changes in insets
    inboxButton.layer.backgroundColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)
    inboxButton.addTarget(self, action: #selector(inboxButtonTapped), for: UIControl.Event.touchUpInside)

    let qrButton = UIButton(type: .system)
    qrButton.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 40, height: 40)) // Set frame of button
    qrButton.setImage(UIImage (named: "QR-code")?.withRenderingMode(.alwaysOriginal), for: .normal)
    qrButton.imageEdgeInsets = .init(top: 5, left: 7, bottom: 5, right: -7) // Minor changes in insets
    qrButton.layer.backgroundColor = #colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)
    qrButton.addTarget(self, action: #selector(qrButtonTapped), for: UIControl.Event.touchUpInside)

    let fixSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    fixSpace.width = 10

    navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: qrButton),fixSpace,UIBarButtonItem(customView: inboxButton)]
    }

    之后,当我运行时,它给出了这个结果:

Right Bar Buttons

关于ios - 无法设置 RightBarButtonItems 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56500456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com