简体   繁体   中英

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. I create uibutton using story board .And i name it Button1,Button2,Button 3

This is my 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 )

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. Here is a link:

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

Autolayout starts around minute 22 of that video. 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. In that first tutorial they will explanin about two autolayout method. Like you need they use UIView to explain

Tutorial 1

Tutorial 2

Hop after read this sure you will get some idea for your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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