我想在UIButtons
内调整图像的大小,但是图像不会调整我尝试的大小。目前图像看起来像this。为什么我无法在images
中调整buttons
的大小?
private func setupButtons() {
planButton.backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
planButton.setImage(UIImage(named: "Plan"), for: .normal)
planButton.alignTextBelow(spacing: 10)
documentButton.backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
documentButton.setImage(UIImage(named: "folder"), for: .normal)
documentButton.alignTextBelow(spacing: 10)
documentationButton.backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
documentationButton.setImage(UIImage(named: "list"), for: .normal)
documentationButton.alignTextBelow(spacing: 10)
visitListButton.backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
visitListButton.setImage(UIImage(named: "people"), for: .normal)
visitListButton.alignTextBelow(spacing: 10)
plancorrectionButton.backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
plancorrectionButton.setImage(UIImage(named: "plancorrection"), for: .normal)
plancorrectionButton.alignTextBelow(spacing: 10)
constructDiaryButton.backgroundColor = UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0)
constructDiaryButton.setImage(UIImage(named: "text"), for: .normal)
constructDiaryButton.alignTextBelow(spacing: 10)
我添加了扩展名,以使标题正确对准图像下方。
extension UIButton{
func alignTextBelow(spacing: CGFloat = 10.0)
{
if let image = self.imageView?.image
{
self.tintColor = .white
let imageSize: CGSize = image.size
self.titleEdgeInsets = UIEdgeInsets(top: spacing, left: -imageSize.width, bottom: -(imageSize.height), right: 0.0)
let labelString = NSString(string: self.titleLabel!.text!)
let titleSize = labelString.size(withAttributes: [NSAttributedString.Key.font: self.titleLabel!.font])
self.imageEdgeInsets = UIEdgeInsets(top: -(titleSize.height + spacing), left: 0.0, bottom: 0.0, right: -titleSize.width)
}
}
}
我尝试使用imageEdgeInsets
,但是我没有想出可行的解决方案。这些图像不会改变我所做的任何事情。理想情况下,图像应全部相同大小。