jj1
jj1

Reputation: 145

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

enter image description here

Thanks in advance !

Upvotes: 1

Views: 3180

Answers (2)

Spike
Spike

Reputation: 588

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.

Upvotes: 1

Guillermo Alvarez
Guillermo Alvarez

Reputation: 1775

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.

Upvotes: 1

Related Questions