【问题标题】:Swift3 - cannot locate UISegmentedControl programmaticallySwift3 - 无法以编程方式定位 UISegmentedControl
【发布时间】:2017-06-14 17:52:09
【问题描述】:

我有一个按钮,我可以使用以下代码轻松找到它:

let btnRewind = UIButton(
    frame:CGRect(
        x:(UIScreen.main.bounds.size.height/2 - 4*UIScreen.main.bounds.size.height/30),
        y:(UIScreen.main.bounds.size.width/2 - UIScreen.main.bounds.size.height/30),
        width:(UIScreen.main.bounds.size.height/15),
        height:(UIScreen.main.bounds.size.height/15)
    )
)

但相同的代码不适用于我的 UISegmentedControl。那是x:0 y:0。我错过了什么:/

var sgmSpeed = UISegmentedControl(
    frame:CGRect(
        x:(UIScreen.main.bounds.size.height/2 - UIScreen.main.bounds.size.height/30),
        y:(UIScreen.main.bounds.size.width/2 - UIScreen.main.bounds.size.height/30),
        width:(UIScreen.main.bounds.size.height/15),
        height:(UIScreen.main.bounds.size.width/15)

    )
)

【问题讨论】:

  • 尝试使用initWithItems 构造函数。文档说,“自动调整大小以适应其内容”。

标签: swift3 uisegmentedcontrol cgrect


【解决方案1】:

感谢 Philip Mills,这个成功了

sgmSpeed.frame = CGRect.init(x:(UIScreen.main.bounds.size.height/2 - UIScreen.main.bounds.size.height/6),
                                 y:(UIScreen.main.bounds.size.width/2 + UIScreen.main.bounds.size.height/15),
                                 width:(UIScreen.main.bounds.size.height/3),
                                 height:(UIScreen.main.bounds.size.width/15))

【讨论】:

    猜你喜欢
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2012-07-23
    • 2011-07-13
    相关资源
    最近更新 更多