【问题标题】:Swift how to move UIButton programmatically?Swift 如何以编程方式移动 UIButton?
【发布时间】:2016-09-28 21:44:54
【问题描述】:

我的 swift 文件中有一个 infoButton UIButton 插座。此按钮最初放置在情节提要中,具有以下约束:

我想将此按钮向下移动 50 像素,具体取决于 NoAds 布尔值是真还是假。

我试过这样做,但我似乎无法移动它。

if NoAds{
            print("No Ads")
            infoButton.center.y = infoButton.center.y - 50
        }else{
            print("Ads")
            loadBanner()
        }

我认为这应该很容易解决?

编辑:该广告是标准的谷歌广告横幅,宽 320,高 50。

【问题讨论】:

  • 您设置了广告视图高度限制?

标签: ios swift uiview uibutton storyboard


【解决方案1】:
  1. 有一个顶部约束的出口

  2. 更改topConstraint.constant

像这样:

if NoAds{
        print("No Ads")
        //topConstraint.constant -= 50
        topConstraint.constant = 50 // Avoid using -= or +=  if this func will call a lot of time
}else{
        print("Ads")

        // set the default constant you want
        topConstraint.constant = 100
        loadBanner()
}

【讨论】:

  • else条件下如何设置按钮默认位置?
  • topConstraint.constant = 你的常数
  • 避免使用-=+= 作为约束常量,如果它们应该只是一个值或另一个值。只需将其设置为 50 或 100,不要对其进行数学运算。或者,您可能会在一个方向上进行太多次调整并错误地移动所有内容。
【解决方案2】:

最好的方法可能是添加约束并动态更改按钮,但如果您不想添加另一个插座并希望动态执行此操作,您可以像这样更改按钮框架的位置:

var X_Position:CGFloat? = startButton.frame.origin.x + 50 //add 50 to move button down page

var Y_Position:CGFloat? = startButton.frame.origin.y

startButton.frame = CGRectMake(X_Position, Y_Position, startButton.frame.width, startButton.frame.height)

这将在新位置重绘按钮。

【讨论】:

  • 在自动布局中更改框架并不可靠。一旦布局更新,框架更改就会被覆盖。
【解决方案3】:

试试这个我希望它会有所帮助这是我正在使用的代码! 我假设你们都设置了顶部、底部、前导、尾随空间等约束

 if NoAds
{
            print("No Ads")
           for item in self.view.constraints
                {
                    if item.firstItem .isKindOfClass(UIButton)
                    {
                        let newField = item.firstItem as! UIButton
                        if newField == buttonName && item.firstAttribute == NSLayoutAttribute.Top
                        {
                            item.constant = -50
                            self.view .layoutIfNeeded()
                        }
                    }
                }
    }else{
            print("Ads")
        for item in self.view.constraints
                {
                    if item.firstItem .isKindOfClass(UIButton)
                    {
                        let newField = item.firstItem as! UIButton
                        if newField == buttonName && item.firstAttribute == NSLayoutAttribute.Top
                        {
                            item.constant = 0
                            self.view .layoutIfNeeded()
                        }
                    }
                }
            loadBanner()
    }

【讨论】:

    【解决方案4】:

    使用IBOutlet 连接到@wilson-xj 描述的约束。

    然后使用以下代码对约束的更改进行动画处理。这将增加一些润色并允许广告很好地滑入/滑出,而不是通过立即改变他们可能与之交互的内容来震动用户。

    view.layoutIfNeeded()
    UIView.animateWithDuration(0.30) { () -> Void in
        self.topConstraint.constant = constraintConstant // 50 or 100 etc
        self.view.layoutIfNeeded()
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-27
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多