[英]Custom iOS UIButton Sub-Class Not Updating Title
I have a subclass of a UIButton
that takes the title label, and puts it under the button's image, as opposed to the right of the image:我有一个
UIButton
的子类,标题为 label,并将其放在按钮图像下方,而不是图像右侧:
final class ImageButton: UIButton {
@IBInspectable var cornerRadius: CGFloat = 8
@IBInspectable var borderColor: UIColor? = .black
private enum Constants {
static let imageSize: CGFloat = 40
static let titleHeight: CGFloat = 12
}
override func titleRect(forContentRect contentRect: CGRect) -> CGRect {
if #available(iOS 15, *) {
return super.titleRect(forContentRect: contentRect)
}
else {
_ = super.titleRect(forContentRect: contentRect)
return CGRect(
x: 0,
y: contentRect.height - Constants.titleHeight,
width: contentRect.width,
height: Constants.titleHeight
)
}
}
override func imageRect(forContentRect contentRect: CGRect) -> CGRect {
if #available(iOS 15, *) {
return super.imageRect(forContentRect: contentRect)
} else {
return CGRect(
x: contentRect.width / 2 - Constants.imageSize / 2,
y: (contentRect.height - titleRect(forContentRect: contentRect).height) / 2 - Constants.imageSize / 2,
width: Constants.imageSize,
height: Constants.imageSize
)
}
}
override var intrinsicContentSize: CGSize {
if #available(iOS 15, *) {
return super.intrinsicContentSize
}
else {
_ = super.intrinsicContentSize
let size = titleLabel?.sizeThatFits(contentRect(forBounds: bounds).size) ?? .zero
let spacing: CGFloat = 12
return CGSize(
width: max(size.width, Constants.imageSize),
height: Constants.imageSize + Constants.titleHeight + spacing
)
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
if #available(iOS 15, *) {
var myConfiguration = UIButton.Configuration.plain()
myConfiguration.imagePlacement = .top
self.configuration = myConfiguration
} else {
titleLabel?.textAlignment = .center
}
}
override func draw(_ rect: CGRect) {
layer.cornerRadius = cornerRadius
layer.masksToBounds = true
layer.borderWidth = 1
layer.borderColor = borderColor?.cgColor
}
}
Trying to change the button's title does not have any effect:尝试更改按钮的标题没有任何效果:
myCustomButton.setTitle("Disable Box Select", for: .normal)
I tried adding:我尝试添加:
myCustomButton.layer.setNeedsLayout() myCustomButton.layer.setNeedsDisplay() myCustomButton.layer.setNeedsLayout() myCustomButton.layer.setNeedsDisplay()
But, nothing seems to change the title of myCustomButton
但是,似乎没有什么改变
myCustomButton
的标题
This is not really an answer, but in response to the OP's comment...这不是一个真正的答案,而是回应 OP 的评论......
Storyboard: Storyboard:
Complete controller code (using your posted ImageButton
class, unedited):完整的 controller 代码(使用您发布的
ImageButton
class,未经编辑):
class CustBtnVC: UIViewController {
@IBOutlet var myCustomButton: ImageButton!
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 15.0, *) {
var cfg = myCustomButton.configuration
cfg?.title = "Test"
myCustomButton.configuration = cfg
} else {
// Fallback on earlier versions
myCustomButton.setTitle("Pre-15 Test", for: .normal)
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if #available(iOS 15.0, *) {
var cfg = myCustomButton.configuration
cfg?.title = "Disable Box Select"
myCustomButton.configuration = cfg
} else {
// Fallback on earlier versions
myCustomButton.setTitle("Pre-15 Disable Box Select", for: .normal)
}
}
}
Note: since your custom button code is setting the button style for iOS 15+ the button .configuration
should be used instead of .setTitle(...)
when running on iOS 15+注意:由于您的自定义按钮代码正在为 iOS 15+ 设置按钮样式,因此在 iOS 15+ 上运行时,应使用按钮
.configuration
而不是.setTitle(...)
On launch:发射时:
after tap on the view:点击视图后:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.