【问题标题】:Swift: Setting width and height within a UIButton classSwift:在 UIButton 类中设置宽度和高度
【发布时间】:2015-02-26 14:56:00
【问题描述】:

所以我的故事板上有一个 60x60 的按钮并为它设置了一个类。

class CustomRedButton: UIButton {

    required init(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        self.layer.cornerRadius         = 0.5 * self.bounds.size.width
        self.backgroundColor            = UIColor.redColor()
        self.titleLabel!.font           = UIFont(name: "Futura", size: 25)
        self.titleLabel!.textAlignment  = .Center

    }
}

我想要做的是根据屏幕大小调整按钮的大小以及其中的字体。

【问题讨论】:

标签: ios iphone class swift uibutton


【解决方案1】:

在初始化时在 ViewController 中使用 view.bounds.maxX 和 view.bounds.maxY。

这就是我正在做的,但它只改变宽度,而不是高度或字体大小。

    button1 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
    button1.center = CGPoint(x: view.bounds.midX - button1.bounds.width / 2 - 5, y: view.bounds.maxY - 160)

    button2 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
    button2.center = CGPoint(x: view.bounds.midX + button1.bounds.width / 2 + 5, y: view.bounds.maxY - 160)

    button3 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
    button3.center = CGPoint(x: view.bounds.midX - button1.bounds.width / 2 - 5, y: view.bounds.maxY - 100)

    button4 = UIButton(frame: CGRect(x: 0, y: 0, width: (view.bounds.maxX / 2) - 10, height: 50))
    button4.center = CGPoint(x: view.bounds.midX + button1.bounds.width / 2 + 5, y: view.bounds.maxY - 100)

要将高度设置为(view.bounds.maxY / 16(或由您决定的任何数字))- 10(按钮之间的间距乘以 2)并更改中心点以反映高度

要对字体大小执行相同操作,请使用 view.bounds.maxX 并将其除以 40 之类的值。你明白了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多