Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding auto layout constraint dynamically in swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 705
    Comment on it

    Hi All,

    You can add auto layout constraints by using Storyboard Interface OR by adding constraints dynamically. There are three ways to add constraint dynamically (as per Apple Inc.)

    Layout Anchor is one of the way is used to add autolayout constraints dynamically.

    Layout Anchors Example :-

    let new_view:UIView! = UIView(frame: CGRectMake(20, 20, 100, 100));
            new_view.backgroundColor = UIColor.redColor();
            new_view.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(new_view);
    
            let margins = view.layoutMarginsGuide
    
            //this line is adding leading of new_view to leading of super view
            new_view.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
    
            //this line is adding top of new_view to top of super view with 20 points constant
            new_view.topAnchor.constraintEqualToAnchor(margins.topAnchor,constant:20.0).active = true
    
            //this line is adding width of new_view with 100.0 points constant
            new_view.widthAnchor.constraintEqualToConstant(100.0).active = true
    
            //this line is adding height of new_view to width of new_view with 20 points constant
            new_view.heightAnchor.constraintGreaterThanOrEqualToAnchor(new_view.widthAnchor, multiplier: 2.0).active = true
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: