大佬教程收集整理的这篇文章主要介绍了Swift - 自定义 UIButton 标题未显示,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建六边形按钮,如果它们以“蜂窝”方式显示,则不会重叠,并且只能选择正确的按钮。我找到了屏蔽 UIView here 的子类,并将它用于我的 UIbutton 类来进行屏蔽。它工作正常!
当我在视图中生成按钮时,按钮标题不可见。研究表明我需要使用
super.layoutSubvIEws()
能够显示标题的文本。我的打印方法显示标题已正确填充并且它与另一个对象类对应,但无论如何,标题不会显示。预期标题为“H”,打印日志显示标题存在于第一个按钮中:
GetTing category: category(date: 2021-07-11 10:29:45 +0000,categoryID: "C1",categorytitle: "Home",shorttitle: "H",categorycolor: UIExtendedSRGBcolorSpace 0.403922 0.643137 0.921569 1,isDefault: falsE)
setupPropertIEs short title: H
Short button title: H
button title: Optional("H")
GetTing category: category(date: 2021-07-11 10:15:50 +0000,categoryID: "C2",categorytitle: "",shorttitle: "",categorycolor: UIExtendedSRGBcolorSpace 0.960784 0.960784 0.960784 1,isDefault: truE)
setupPropertIEs short title:
Short button title:
button title: nil
我什至尝试向 UIbutton 子视图添加另一个标签,并以相同的方法将此标签置于最前面,但它也不会显示。
是否有另一种方法可以强制自定义按钮显示标题文本,或者我是否必须在与按钮相同的位置创建带有标题的 UILabel 并将其放置在按钮上方的图层中?这种方法不是首选...
是不是因为UIbutton被多边形遮住了?
六角按钮类:
class Hexabutton: UIbutton {
var path: UIBezIErPath!
var category: category!
init(cat: category) {
super.init(frame: CGRect())
self.category = cat
self.setupPropertIEs()
//self.layoutSubvIEws()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.setupPropertIEs()
//self.layoutSubvIEws()
fatalError("init(coder:) has not been implemented")
}
overrIDe func awakeFromNib() {
addTarget(self,action: #SELEctor(touchDown),for: .touchDown)
self.setupPropertIEs()
//self.layoutSubvIEws()
}
private func setupPropertIEs(){
self.titleLabel?.Font = UIFont.boldSystemFont(ofSize: 10)
self.settitlecolor(UIcolor.white,for: .normal)
self.settitle(category.shorttitle,for: .normal)
print("setupPropertIEs short title: \(category.shorttitlE)")
}
overrIDe func layoutSubvIEws() {
super.layoutSubvIEws()
self.titleLabel?.Font = UIFont.boldSystemFont(ofSize: 10)
self.settitlecolor(UIcolor.white,for: .normal)
}
overrIDe func draw(_ rect: CGRect) {
let linewidth: CGfloat = 5
path = self.roundedpolygonPath(rect: self.bounds,linewidth: linewidth,sIDes: 6,cornerRadius: 10,rotationOffset: CGfloat(.pi / 2.0))
let shapeLayer = CAShapeLayer()
shapeLayer.strokecolor = category.categorycolor.cgcolor
shapeLayer.fillcolor = category.categorycolor.cgcolor
shapeLayer.path = path.cgPath
layer.addSublayer(shapeLayer)
}
@objc func touchDown(button: Hexabutton,event: UIEvent) {
if let touch = event.touches(for: button)?.first {
let LOCATIOn = touch.LOCATIOn(in: button)
if path.contains(LOCATIOn) == false {
button.cancelTracking(with: nil)
}
}
}
public func roundedpolygonPath(rect: CGRect,linewidth: CGfloat,sIDes: NSInteger,cornerRadius: CGfloat,rotationOffset: CGfloat = 0) -> UIBezIErPath {
let path = UIBezIErPath()
let theta: CGfloat = CGfloat(2.0 * .pi) / CGfloat(sIDes) // How much to turn at every corner
let wIDth = min(rect.size.wIDth,rect.size.height) // WIDth of the square
let center = CGPoint(x: rect.origin.x + wIDth / 2.0,y: rect.origin.y + wIDth / 2.0)
// Radius of the circle that encircles the polygon
// Notice that the radius is adjusted for the corners,that way the largest outer
// dimension of the resulTing shape is always exactly the wIDth - linewidth
let radius = (wIDth - linewidth + cornerRadius - (cos(theta) * cornerRadius)) / 2.0
// Start drawing at a point,which by default is at the right hand edge
// but can be offset
var angle = CGfloat(rotationOffset)
let corner = CGPoint(x: center.x + (radius - cornerRadius) * cos(anglE),y: center.y + (radius - cornerRadius) * sin(anglE))
path.move(to: CGPoint(x: corner.x + cornerRadius * cos(angle + theta),y: corner.y + cornerRadius * sin(angle + theta)))
for _ in 0..<sIDes {
angle += theta
let corner = CGPoint(x: center.x + (radius - cornerRadius) * cos(anglE),y: center.y + (radius - cornerRadius) * sin(anglE))
let tip = CGPoint(x: center.x + radius * cos(anglE),y: center.y + radius * sin(anglE))
let start = CGPoint(x: corner.x + cornerRadius * cos(angle - theta),y: corner.y + cornerRadius * sin(angle - theta))
let end = CGPoint(x: corner.x + cornerRadius * cos(angle + theta),y: corner.y + cornerRadius * sin(angle + theta))
path.addline(to: start)
path.addQuadCurve(to: end,controlPoint: tip)
}
path.close()
// Move the path to the correct origins
let bounds = path.bounds
let transform = CGAffinetransform(translationX: -bounds.origin.x + rect.origin.x + linewidth / 2.0,y: -bounds.origin.y + rect.origin.y + linewidth / 2.0)
path.apply(transform)
return path
}
}
有人能指出我正确的方向吗,我错过了什么?谢谢
当你覆盖平局时 -
override func draw(_ rect: CGRect) {
}
和
您没有调用 super.draw(rect)
,这意味着您不会得到任何 UIButton
的绘图。
您可以删除 draw(_ rect: CGRect)
实现并将形状层代码移到 layoutSubviews()
方法中。
此外,您不应该在每次调用时都添加一个新层。考虑以下之一 -
这将使您恢复 UIButton
的内置绘图。
此外,当您添加图层时,您正在调用 addSublayer
,它将在按钮的图层层次结构中的所有其他内容之上添加您的子图层,从而完全覆盖由按钮的默认绘图呈现的任何内容。
考虑使用 insertSublayer
变体 - at: index
OR below: sublayer
等
以上是大佬教程为你收集整理的Swift - 自定义 UIButton 标题未显示全部内容,希望文章能够帮你解决Swift - 自定义 UIButton 标题未显示所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。