UINavigationItem ile kullanılabilecek bir fonksiyon hazırladım.
NavigationController altında yer alan viewcontroller'ların rightBarButtonItem propertysine set ederek kullanabilirsin.
Badge'i update etmek istediğinde, fonksiyonu yeniden çağırarak button objesinin yeni badge numarasıyla yeniden oluşturulmasını sağlayabilirsin.
import UIKit
class ViewController: UIViewController {
var badgeCounter:UInt = 0
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.rightBarButtonItem = barButtonItemWithBadge(badgeNum: 0, buttonHeight: 30.0, buttonImage: UIImage(named: "mailIcon"), selector: #selector(buttonAction))
}
@objc func buttonAction() {
print("do something")
self.badgeCounter += 1
self.navigationItem.rightBarButtonItem = barButtonItemWithBadge(badgeNum: self.badgeCounter, buttonHeight: 30.0, buttonImage: UIImage(named: "mailIcon"), selector: #selector(buttonAction))
}
func barButtonItemWithBadge(badgeNum: UInt, buttonHeight: CGFloat, buttonImage: UIImage!, selector: Selector) -> UIBarButtonItem {
let size:CGFloat = (buttonHeight * 0.5)
let fontColor:UIColor = .white
let backgroundColor:UIColor = .red
let borderWidth:CGFloat = (size * 0.1)
let borderColor:UIColor = .white
let fontName:String = "Avenir-Heavy"
let button = UIButton(type: .custom)
button.setImage(buttonImage, for: .normal)
button.imageView?.contentMode = .scaleAspectFit
button.addTarget(self, action: selector, for: .touchUpInside)
let rightBarButton = UIBarButtonItem(customView: button)
if let parrentView = rightBarButton.customView {
parrentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
parrentView.widthAnchor.constraint(equalTo: parrentView.heightAnchor),
parrentView.heightAnchor.constraint(equalToConstant: buttonHeight)
])
if badgeNum > 0 {
var badgeSize:CGSize = CGSize(width: size + (borderWidth * 4), height: size + (borderWidth * 4))
if let textFont = UIFont(name: fontName, size: ceil(size * 0.8)) {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
let textFontAttributes:[NSAttributedStringKey : Any] = [
NSAttributedStringKey.font: textFont,
NSAttributedStringKey.foregroundColor: fontColor,
NSAttributedStringKey.paragraphStyle: paragraphStyle
]
let text:String = "\(badgeNum)"
let calculatedTextSize = (text as NSString).size(withAttributes: textFontAttributes)
if calculatedTextSize.width > size {
badgeSize = CGSize(width: calculatedTextSize.width + (borderWidth * 5), height: size + (borderWidth * 4))
}
let attributedText = NSAttributedString(string: text, attributes: textFontAttributes)
let holderView:UIView = UIView()
holderView.isUserInteractionEnabled = false
holderView.backgroundColor = backgroundColor
holderView.layer.cornerRadius = badgeSize.height * 0.5
holderView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
holderView.layer.shadowColor = UIColor.black.cgColor
holderView.layer.shadowRadius = 0.2
holderView.layer.shadowOpacity = 0.5
holderView.layer.borderWidth = borderWidth
holderView.layer.borderColor = borderColor.cgColor
holderView.translatesAutoresizingMaskIntoConstraints = false
parrentView.addSubview(holderView)
NSLayoutConstraint.activate([
holderView.widthAnchor.constraint(equalToConstant: badgeSize.width),
holderView.heightAnchor.constraint(equalToConstant: badgeSize.height),
holderView.trailingAnchor.constraint(equalTo: parrentView.trailingAnchor, constant: (badgeSize.width * 0.4)),
holderView.topAnchor.constraint(equalTo: parrentView.topAnchor, constant: -(badgeSize.height * 0.2))
])
let badge = UILabel()
badge.attributedText = attributedText
badge.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
badge.layer.shadowColor = UIColor.black.cgColor
badge.layer.shadowRadius = 0.0
badge.layer.shadowOpacity = 0.4
badge.translatesAutoresizingMaskIntoConstraints = false
holderView.addSubview(badge)
NSLayoutConstraint.activate([
badge.widthAnchor.constraint(equalToConstant: badgeSize.width),
badge.heightAnchor.constraint(equalToConstant: badgeSize.height),
badge.centerXAnchor.constraint(equalTo: holderView.centerXAnchor),
badge.centerYAnchor.constraint(equalTo: holderView.centerYAnchor)
])
parrentView.layoutIfNeeded()
}
}
}
return rightBarButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}