按钮点击标签在左上角上升

时间:2015-11-19 14:16:45

标签: swift autolayout

我已为标签设置了自动布局,因此它应位于我的按钮正上方,但是当我点击按钮时,标签会在左上角上升。我是一个全新的编码初学者,所以想到我可能只是错过了一些非常简单的东西?我想在我的代码中我需要某种方式来告诉标签在点击后的位置?

这是我的按钮操作代码:

        nextStagetwo.textAlignment = NSTextAlignment.Center
        nextStagetwo.userInteractionEnabled = true
        nextStagetwo.text = "Go to next stage"
        self.view.addSubview(nextStagetwo)

        let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
        nextStagetwo.addGestureRecognizer(gestureRecognizer)

    }


}


func handleTap(gestureRecognizer: UIGestureRecognizer) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc: AnyObject! = storyboard.instantiateViewControllerWithIdentifier("stagethree")
    self.presentViewController(vc as! UIViewController, animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:0)

在Xcode 7中运行您的应用程序并选择调试器视图,然后使用工具提示“调试视图层次结构”(调试器工具栏右侧的第三个按钮)选择调试器工具栏按钮。这将显示为屏幕上的每个项目计算的自动布局。您可以根据需要在此视图中剥离图层。将这些值与该项目及其容器的自动布局约束进行比较。

另外,请确保您的故事板在Xcode Interface Builder中没有任何自动布局警告。

答案 1 :(得分:0)

我将此添加到代码中,这是我的代码中缺少的内容:

let nextStagetwo = UILabel(frame: CGRectMake(250, 250, 250, 250))
nextStagetwo.center = CGPointMake(210, 200)