Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift如何以编程方式移动UIButton?_Ios_Swift_Uiview_Uibutton_Storyboard - Fatal编程技术网

Ios Swift如何以编程方式移动UIButton?

Ios Swift如何以编程方式移动UIButton?,ios,swift,uiview,uibutton,storyboard,Ios,Swift,Uiview,Uibutton,Storyboard,我的swift文件中有一个infoButtonui按钮插座。此按钮最初放置在故事板中,具有以下约束: 我想将此按钮向下移动50像素,这取决于NoAdsboolean是true还是false 我试过这样做,但我似乎无法移动它 if NoAds{ print("No Ads") infoButton.center.y = infoButton.center.y - 50 }else{ print("Ads")

我的swift文件中有一个
infoButton
ui按钮插座。此按钮最初放置在故事板中,具有以下约束:

我想将此按钮向下移动50像素,这取决于
NoAds
boolean是true还是false

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

if NoAds{
            print("No Ads")
            infoButton.center.y = infoButton.center.y - 50
        }else{
            print("Ads")
            loadBanner()
        }
我想这应该是一个简单的解决办法

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

  • 为顶部约束设置一个出口

  • 更改
    topConstraint.常量

  • 像这样:

    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()
    }
    

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

    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)
    

    这将在新位置重新绘制按钮。

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

     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()
        }
    

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

    然后使用以下代码设置约束更改的动画。这将增加一些润色,并允许广告很好地滑入/滑出,而不是通过即时改变用户可能与之互动的内容来扰乱用户

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

    如何在else条件下设置按钮的默认位置?topConstraint.constant=您的ConstantAvid使用约束常量的
    -=
    +=
    ,如果它们只能是一个或另一个值。把它设为50或100,不要做数学运算。或者,在某些情况下,您可能会在一个方向上进行多次调整,并错误地移动所有内容。您设置了ads视图高度约束?在自动布局中更改帧不可靠。一旦布局得到更新,框架更改就会被覆盖。