如何调整导航栏按钮图片大小

问题描述 投票:0回答:1

我已经尝试了其他堆栈溢出的答案,但似乎并不奏效,我不确定是否在swift 5中发生了什么变化。但这里是我用来创建右栏按钮的代码。我按照youtube上的教程,然后在stackoverflow上寻找解决方案,但似乎都没有用。因为我已经将按钮的框架设置为34x34,但图像似乎只是在整个导航栏上延伸。

        //Adding Unmatch Button
        let unmatchBtn = UIButton(type: .system)
        unmatchBtn.setImage(UIImage(named: "heartbreak")?.withRenderingMode(.alwaysOriginal), for: .normal )
        unmatchBtn.frame = CGRect(x: 0, y: 0, width: 34, height: 34)

        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: unmatchBtn)

enter image description here

编辑。

在答案的帮助下,现在导航栏上的按钮已经很合适了,但是布局已经乱了。它左边的东西是我做的一个自定义视图,它被推到了左边。enter image description here

ios swift uinavigationbar frame cgrect
1个回答
1
投票
let item = UIBarButtonItem(image: #imageLiteral(resourceName: ImageNameHere), style: .plain, target: self, action: #selector(action))

@objc func action() {

}
© www.soinside.com 2019 - 2024. All rights reserved.