以编程方式设置UIButton

时间:2017-02-16 08:41:08

标签: swift uibutton frame

我有一个我创建的自定义UIButton。在按钮内部,我有2个标签,一个在另一个之上。最上面一个是标题,所以它有点大,底部有点小。 我的目标是让两个标签完全覆盖整个按钮,如下所示: 顶部标签将覆盖按钮的顶部2/3部分,底部将覆盖按钮的其余1/3部分。 我的目标离目标不远,但我得到了奇怪的行为 - 标签有点偏离按钮,在某些情况下它们会消失(我可以点击按钮但看不到标签)。 这是我的自定义UIButton供参考,我希望这段代码可以帮助人们不管我的问题:

class ButtonWithStats: UIButton {

var num: Int
var name: String
var nameLabel: UILabel?
var numLabel: UILabel?
required init?(coder aDecoder: NSCoder) {
   // fatalError("init(coder:) has not been implemented")
    self.num = 0
    self.name = ""

    self.numLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0)) //just to init the labels.
    self.nameLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
    self.numLabel?.textAlignment = .center
    self.nameLabel?.textAlignment = .center
    self.numLabel?.textColor = UIColor.purple
    self.nameLabel?.textColor = UIColor.black
    self.numLabel?.font = UIFont.init(name: "Helvetica", size: 11)
    self.nameLabel?.font = UIFont.init(name: "Helvetica", size: 13)
    super.init(coder: aDecoder)
}

func setButton(numInput: Int, nameInput: String){
    self.num = numInput
    self.name = nameInput
    self.setLabels()
}

private func setLabels(){

    numLabel?.text = String(self.num)
    nameLabel?.text = self.name
    let widthForName = self.frame.width
    let heightForName = self.frame.height * 2 / 3
    nameLabel?.center = self.center
    nameLabel?.frame = CGRect(origin: self.frame.origin, size: CGSize(width: widthForName, height: heightForName))
    let widthForNum = self.frame.width
    let heightForNum = self.frame.height * 1 / 3
    let yForNum = (self.frame.height * 2 / 3) //+ self.frame.origin.y
    numLabel?.frame = CGRect(x: self.frame.origin.x, y: yForNum, width: widthForNum, height: heightForNum)
    self.addSubview(nameLabel!)
    self.addSubview(numLabel!)
    print("@@@@@@@@@@@@@@@@@@@@@@@@@")
    print("Button \(self.nameLabel?.text) frame is: \(self.frame)")
    print("Label frame is: \(self.nameLabel?.frame)")
    print("Num frame is: \(self.numLabel?.frame)")
    print("@@@@@@@@@@@@@@@@@@@@@@@@@")
}
}

我的控制台打印此数据:

@@@@@@@@@@@@@@@@@@@@@@@@@
Button Optional("button one") frame is: (257.5, 0.0, 64.5, 33.0)
Label frame is: Optional((257.5, 0.0, 64.5, 22.0))
Num frame is: Optional((257.5, 22.0, 64.5, 11.0))
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
Button Optional("Button two") frame is: (129.0, 0.0, 64.0, 33.0)
Label frame is: Optional((129.0, 0.0, 64.0, 22.0))
Num frame is: Optional((129.0, 22.0, 64.0, 11.0))
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
Button Optional("Button three") frame is: (0.0, 0.0, 64.5, 33.0)
Label frame is: Optional((0.0, 0.0, 64.5, 22.0))
Num frame is: Optional((0.0, 22.0, 64.5, 11.0))
@@@@@@@@@@@@@@@@@@@@@@@@@

1 个答案:

答案 0 :(得分:1)

而不是添加2个标签,如何在Message: CSRF attack detected. Exception type: CMS.Protection.Web.UI.CsrfException Stack trace: at CMS.Protection.Web.UI.CsrfProtection.ThrowCsrfException(Exception innerException) at CMS.Protection.Web.UI.CsrfProtection.OnPostMapRequestHandlerExecute(Object sender, EventArgs eventArgs) at CMS.Base.AbstractHandler.CallEventHandler[TArgs](EventHandler`1 h, TArgs e) at CMS.Base.AbstractHandler.Raise[TArgs](String partName, List`1 list, TArgs e, Boolean important) at CMS.Base.SimpleHandler`2.RaiseExecute(TArgs e) at CMS.Base.SimpleHandler`2.RaiseExecute(TArgs e) at CMS.Base.SimpleHandler`2.StartEvent(TArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) Message: Error occurred during a cryptographic operation. Exception type: System.Security.Cryptography.CryptographicException Stack trace: at System.Web.Security.Cryptography.HomogenizingCryptoServiceWrapper.HomogenizeErrors(Func`2 func, Byte[] input) at CMS.Protection.Web.UI.CsrfProtection.OnPostMapRequestHandlerExecute(Object sender, EventArgs eventArgs) numberOfLines设置UIButtons

textLabel

或者,添加垂直button.titleLabel?.numberOfLines = 2 ,将2个标签作为子视图,并设置UIStackView

stackView.frame = button.bounds

您可以调整let stackView = UIStackView(arrangedSubviews: [numLabel, nameLabel]) stackView.frame = buttonView.bounds addSubview(stackView) / stackView.spacing等,根据需要定位标签

要使layoutMargins高度为numLabel的两倍,您可以使用设置约束...

nameLabel

我建议您在storyboard / xib中执行此操作,但如果您需要以编程方式执行此操作,则可能会发现创建一个xib来尝试numLabel.heightAnchor.constraint(equalTo: nameLabel.heightAnchor, multiplier: 2) 设置很有用。