【发布时间】:2019-11-10 22:46:02
【问题描述】:
所以我试图将一个按钮设置为我的 UINavigationItem 的 titleView,带有标题和图像。我正在使用以下类别来对齐图像下方的标题:
extension UIButton {
func centerButtonVertically(padding: CGFloat = 6.0) {
guard
let imageViewSize = self.imageView?.frame.size,
let titleLabelSize = self.titleLabel?.frame.size else {
return
}
let totalHeight = imageViewSize.height + titleLabelSize.height + padding
self.imageEdgeInsets = UIEdgeInsets(
top: -(totalHeight - imageViewSize.height),
left: 0.0,
bottom: 0.0,
right: -titleLabelSize.width
)
self.titleEdgeInsets = UIEdgeInsets(
top: 0.0,
left: -imageViewSize.width,
bottom: -(totalHeight - titleLabelSize.height),
right: 0.0
)
self.contentEdgeInsets = UIEdgeInsets(
top: 0.0,
left: 0.0,
bottom: titleLabelSize.height,
right: 0.0
)
}
}
我正在使用 Kingfisher 下载、调整大小和圆整图像:
override func viewDidLoad() {
super.viewDidLoad()
let rounded = RoundCornerImageProcessor(cornerRadius: 15)
let downsampling = ResizingImageProcessor(referenceSize: CGSize(width: 30.0, height: 30.0), mode: .aspectFit)
matchAvatarButton.setTitle(self.match.shortName, for: .normal)
matchAvatarButton.contentMode = .scaleAspectFit
matchAvatarButton.kf.setImage(with: self.match.pictureUrls[0] as Resource, for: .normal, placeholder: nil, options: [.processor(downsampling), .processor(rounded)])
matchAvatarButton.centerButtonVertically()
...
}
请注意,该按钮在故事板中设置为我的导航栏的 titleView,然后我通过 viewDidLoad 中的出口引用修改该按钮。
但结果与我想要达到的结果相去甚远:
图像保持矩形,我希望它是一个圆圈。由于某些奇怪的原因,我没有看到标题。
知道我错过了什么吗?
【问题讨论】:
标签: ios uibutton uikit uinavigationbar kingfisher