简体   繁体   English

快速为UIButton添加约束

[英]Adding constraints for UIButton in swift

I just create a app ,that have 3 UIButton in vertical mode one by one.I add some autolayout to fix my image .but it differ for different simulator device. 我只是创建了一个应用程序,该应用程序一个接一个地在垂直模式下具有3个UIButton 。我添加了一些自动布局来修复我的图像。 I create uibutton using story board .And i name it Button1,Button2,Button 3 我使用故事板创建uibutton,并将其命名为Button1,Button2,Button 3

This is my viewcontrller.swift 这是我的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!

I too see some post to solve constraint .Like this code: (for button1 alone here i added ) 我也看到一些解决约束的帖子。就像下面的代码:(我仅在这里为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))

I need to keep my button like this image 我需要保持像这样的按钮

在此处输入图片说明

Thanks in advance ! 提前致谢 !

Autolayout is difficult to explain in words but the best tutorial on autolayout I have ever seen is lecture 9 in the most recent Stanford iOS course. 用文字很难解释自动布局,但是我见过的关于自动布局的最佳教程是斯坦福iOS最新课程中的第9讲。 Here is a link: 这里是一个链接:

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

Autolayout starts around minute 22 of that video. 自动布局会在该视频的第22分钟左右开始。 I highly recommend it. 我强烈推荐它。

Ok so you need to set three button in vertical mode like this.For that you can make use this two tutorial by raywenderlich. 好的,因此您需要在垂直模式下设置三个按钮,为此,您可以使用raywenderlich的这两个教程。 In that first tutorial they will explanin about two autolayout method. 在第一个教程中,他们将解释两种自动布局方法。 Like you need they use UIView to explain 就像您需要他们使用UIView来解释

Tutorial 1 教程1

Tutorial 2 教程2

Hop after read this sure you will get some idea for your app. 仔细阅读此内容后,请确保您会对您的应用有所了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM