2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Auto Layout】制約のアウトレット接続【Xcode8.x】

Last updated at Posted at 2017-01-04

実は制約も部品のようにアウトレット接続が可能なのです。自分的メモなのでザックリ掲載。

制約をコードから操作するシンプルなデモ

黄色いラベルの横サイズとスーバービューとの上マージンを操作しています。緑のLabelには黄色いLabelとのマージンとEqual Bottomが指定されているので黄色いラベルの移動に伴って一緒に移動します。
demo.gif

ViewController.swift


//
//  ViewController.swift
//  Constraint
//

import UIKit

class ViewController: UIViewController {
    
    
    var toggle1:Bool = true
    var toggle2:Bool = true
    var toggle3:Bool = true
    
    @IBOutlet weak var label1:UILabel!
    
    @IBOutlet var constraintLabelWidth:NSLayoutConstraint!
    @IBOutlet var constraintLabelTopMargin:NSLayoutConstraint!
    
    @IBAction func changeConstraint(_ sender:UIButton){
        
        toggle1 = toggle1 ? false : true
        let value:CGFloat = toggle1 ? 100 : -100
        
        constraintLabelWidth.constant += value
        
    }
    
    @IBAction func changeConstraintWithAnimation(_ sender:UIButton){
        
        toggle2 = toggle2 ? false : true
        let value:CGFloat = toggle2 ? 100 : -100
        
        self.view.removeConstraint(constraintLabelWidth)
        constraintLabelWidth.constant += value
        label1.addConstraint(constraintLabelWidth)
        UIView.animate(
            withDuration: 1.0,
            delay:0.5,
            options:UIViewAnimationOptions.curveEaseOut,
            animations: {() -> Void in
                self.view.layoutIfNeeded()
            },
            completion: nil
        )
    }
    
    @IBAction func changeConstraintTopMarginWithAnimation(_ sender:UIButton){
        
        toggle3 = toggle3 ? false : true
        let value:CGFloat = toggle3 ? 100 : -100
        
        self.view.removeConstraint(constraintLabelTopMargin)
        constraintLabelTopMargin.constant += value
        view.addConstraint(constraintLabelTopMargin)
        UIView.animate(
            withDuration: 0.5,
            delay:0.0,
            options:UIViewAnimationOptions.curveEaseOut,
            animations: {() -> Void in
                self.view.layoutIfNeeded()
            },
            completion: nil
        )
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


アウトレット・アクション接続
4.png

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?