我已经尝试了其他堆栈溢出的答案,但似乎并不奏效,我不确定是否在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)
编辑。
let item = UIBarButtonItem(image: #imageLiteral(resourceName: ImageNameHere), style: .plain, target: self, action: #selector(action))
@objc func action() {
}