gpt4 book ai didi

swift - 这可以改变条形按钮的 "Y"位置吗?

转载 作者:行者123 更新时间:2023-11-28 05:42:42 24 4
gpt4 key购买 nike

enter image description here
这是我的 UIBarButtonItem 代码,我添加了 imageInsets 以向其添加“Y”位置,但它不起作用。

let leftBarButtonItem = UIBarButtonItem()
leftBarButtonItem.imageInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0)
leftBarButtonItem.tintColor = .red
navigationItem.setRightBarButton(leftBarButtonItem, animated: true)

最佳答案

这件事太简单了。您可以尝试以下方法:

直接使用 UIBarButtonItem :

let leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_2"), style: UIBarButtonItem.Style.plain, target: self, action: #selector(rightBtnClicked(_:)))
leftBarButtonItem.imageInsets = UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 0)
leftBarButtonItem.tintColor = .red
self.navigationItem.setRightBarButton(leftBarButtonItem, animated: true)

或通过自定义按钮:

let rightButton = UIButton(type: UIButton.ButtonType.system)
rightButton.backgroundColor = .clear
rightButton.tintColor = .red
rightButton.frame = CGRect(origin: CGPoint(x: 10, y: 10), size: CGSize(width: 40, height: 40))
//rightButton.frame = CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 40, height: 40))
//rightButton.imageEdgeInsets = UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 0)
//rightButton.contentVerticalAlignment = .bottom
rightButton.setImage(UIImage(named: "icon_1"), for: UIControl.State.normal)
rightButton.addTarget(self, action: #selector(rightBtnClicked(_:)), for: UIControl.Event.touchUpInside)

let btnView = UIView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 50, height: 50)))
btnView.backgroundColor = .clear
btnView.addSubview(rightButton)

let barBtn = UIBarButtonItem(customView: btnView)
self.navigationItem.setRightBarButton(barBtn, animated: true)

关于swift - 这可以改变条形按钮的 "Y"位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56018457/

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