在 swift 中为 UIButton 添加约束

Adding constraints for UIButton in swift

我刚刚创建了一个应用程序,在垂直模式下有 3 个 UIButton,其中一个通过 one.I 添加一些自动布局来修复我的图像。但它因不同的模拟器设备而异。我使用故事板创建 uibutton。我将其命名为 Button1,Button2,Button 3

这是我的viewcontrller.swift

class ViewController: UIViewController {
    @IBOutlet weak var button1: UIButton!
    @IBOutlet weak var button2: UIButton!
    @IBOutlet weak var button3: UIButton!

    @IBOutlet weak var titleL: UILabel!

我也看到了一些 post 来解决约束。像这样的代码:(我在这里单独添加了 button1

self.view.addConstraint(NSLayoutConstraint(item: button1 , attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
        self.view.addConstraint(NSLayoutConstraint(item: button1 , attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
        self.view.addConstraint(NSLayoutConstraint(item: button1 , attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))

我需要像这张图片一样保留我的按钮

提前致谢!

Autolayout 很难用语言解释,但我见过的最好的 autolayout 教程是斯坦福 iOS 最近课程的第 9 讲。这是一个 link:

https://itunes.apple.com/us/course/8.-view-controller-lifecycle/id961180099?i=335430612&mt=2

自动布局在该视频的第 22 分钟左右开始。我强烈推荐它。

好的,所以您需要在垂直模式下设置三个按钮,例如 this.For,您可以使用 raywenderlich 的这两个教程。在第一个教程中,他们将解释两种自动布局方法。就像你需要他们使用 UIView 来解释

Tutorial 1

Tutorial 2

阅读本文后,您一定会对您的应用有所了解。