ios - 包含在 UIBarButtonItem 中时增加 UIButton 的固有大小
问题描述
我创建了一个 UIButton 子类,其视图位于按钮边界之外。
它看起来如屏幕截图所示:
如您所见,徽章位于按钮之外。它的中心固定在按钮的右上角。
问题是当按钮放在 a 内时UIBarButtonItem
,徽章会被剪掉。
视图层次结构检查器显示了原因:
我如何UIBarButtonItem
与按钮的大小比按钮本身大的沟通?我必须覆盖的哪些属性UIButton
?
我用于按钮的代码:
import UIKit
import BadgeSwift
@objc public class BadgeButton: UIButton {
private lazy var badge: BadgeSwift = {
let b = BadgeSwift()
b.textColor = .white
b.insets = CGSize(width: 0, height: 0)
b.font = UIFont.preferredFont(forTextStyle: .caption1)
return b
}()
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required public init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
addSubview(badge)
badge.text = "1"
}
public override func layoutSubviews() {
super.layoutSubviews()
clipsToBounds = false
badge.sizeToFit()
badge.center = center
badge.center = CGPoint(x: bounds.origin.x + bounds.size.width, y: bounds.origin.y)
}
}
对于 Badge 视图,我使用以下库:BadgeSwift Library
解决方案
您可以为您创建图层UIBarButtonItem
并对其进行整形。
创建 .swift 文件并粘贴此代码
import UIKit
extension CAShapeLayer {
func drawRoundedRect(rect: CGRect, andColor color: UIColor, filled: Bool) {
fillColor = filled ? color.cgColor : UIColor.white.cgColor
strokeColor = color.cgColor
path = UIBezierPath(roundedRect: rect, cornerRadius: 7).cgPath
}
}
private var handle: UInt8 = 0;
extension UIBarButtonItem {
var badgeLayer: CAShapeLayer? {
if let b: AnyObject = objc_getAssociatedObject(self, &handle) as AnyObject? {
return b as? CAShapeLayer
} else {
return nil
}
}
func setBadge(text: String?, withOffsetFromTopRight offset: CGPoint = CGPoint.zero, andColor color:UIColor = UIColor.red, andFilled filled: Bool = true, andFontSize fontSize: CGFloat = 11)
{
badgeLayer?.removeFromSuperlayer()
if (text == nil || text == "") {
return
}
addBadge(text: text!, withOffset: offset, andColor: color, andFilled: filled)
}
func addBadge(text: String, withOffset offset: CGPoint = CGPoint.zero, andColor color: UIColor = UIColor.red, andFilled filled: Bool = true, andFontSize fontSize: CGFloat = 11)
{
guard let view = self.value(forKey: "view") as? UIView else { return }
var font = UIFont.systemFont(ofSize: fontSize)
if #available(iOS 9.0, *) { font = UIFont.monospacedDigitSystemFont(ofSize: fontSize, weight: UIFont.Weight.regular) }
let badgeSize = text.size(withAttributes: [NSAttributedString.Key.font: font])
// Initialize Badge
let badge = CAShapeLayer()
let height = badgeSize.height;
var width = badgeSize.width + 2 /* padding */
//make sure we have at least a circle
if (width < height) {
width = height
}
//x position is offset from right-hand side
let x = view.frame.width - width + offset.x
let badgeFrame = CGRect(origin: CGPoint(x: x, y: offset.y), size: CGSize(width: width, height: height))
badge.drawRoundedRect(rect: badgeFrame, andColor: color, filled: filled)
view.layer.addSublayer(badge)
// Initialiaze Badge's label
let label = CATextLayer()
label.string = text
label.alignmentMode = CATextLayerAlignmentMode.center
label.font = font
label.fontSize = font.pointSize
label.frame = badgeFrame
label.foregroundColor = filled ? UIColor.white.cgColor : color.cgColor
label.backgroundColor = UIColor.clear.cgColor
label.contentsScale = UIScreen.main.scale
badge.addSublayer(label)
// Save Badge as UIBarButtonItem property
objc_setAssociatedObject(self, &handle, badge, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
func removeBadge() {
badgeLayer?.removeFromSuperlayer()
}
}
从你的 ViewController 使用
//ADD BADGE
self.yourBarBtn.addBadge(text: String(7))
//REMOVE BADGE
self.pendingBarBtn.removeBadge()
推荐阅读
- ios - 导航栏下拉按钮?
- python - ValueError: invalid literal for int() with base 10: '[' --- 请解释错误以及如何解决?
- angularjs - 将文档从 angularjs 上传到 springboot
- swift - SKAction.move(to: ...) 和 SKAction.move(by: ...) 为我做同样的事情
- keras - 矩阵大小不兼容的keras
- python - 考虑到 upload_to() 函数,如何获取 FieldField 的路径
- spring-boot - Spring Boot、Debian、Embedded Tomcat 关闭外部端口
- angular - 在 Ngrx-nx Monorepo 中管理共享样式和资产
- mysql - 创建 MySQL 时出错 ERROR 1067 (42000): 'end' 的默认值无效
- sql-server - ORDER BY 中的字段影响窗口函数的结果