我想要设置背景层的位置,如图片所示。
我试图设置图层的位置,但是我不能。
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)]
}
我希望背景层的位置与图片中的位置相似。
答案 0 :(得分:0)
我对您的代码进行了一些更改。
您按钮的图像尺寸应类似于:
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)]
}
在此之后,当我跑步时,它会给我以下结果:
答案 1 :(得分:0)
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.contentEdgeInsets = .init(top: 0, left: -37, bottom: 0, right: 20)
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.contentEdgeInsets = .init(top: 0, left: -20, bottom: 0, right: 0)
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)]
}
只需要设置contentEdgeInsets。