func initButtonItem(){
// Create a UIImage for the desired image
let image = UIImage(named: "icon_share")
// Define the desired size for the image
let imageSize = CGSize(width: Constants.TOP_IMAGE_SIZE, height: Constants.TOP_IMAGE_SIZE)
// Resize the image while preserving its aspect ratio
let resizedImage = image?.resizableImage(withCapInsets: .zero, resizingMode: .stretch).withRenderingMode(.alwaysOriginal).scalePreservingAspectRatio(targetSize: imageSize)
// Create a UIButton with the desired image
let customButton = UIButton(type: .custom)
customButton.setImage(resizedImage, for: .normal)
customButton.addTarget(self, action: #selector(actionMethod), for: .touchUpInside)
// Create a UIBarButtonItem with the custom button
let barButtonItem = UIBarButtonItem(customView: customButton)
// Create a note UIBarButtonItem
let barButtonItem2 = UIBarButtonItem(title: "标题", style: .plain, target: self, action: #selector(actionNote))
// Set the custom font for the note button
let customFont = UIFont.systemFont(ofSize: 16.0)
barButtonItem2.setTitleTextAttributes([NSAttributedString.Key.font: customFont], for: .normal)
// Set the rightBarButtonItems
navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]
}
viewDidLoad调用即可