Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Auto Layout Using NSLayoutConstraint In Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 576
    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.)

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

    NSLayoutConstraint 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);
    
    
            NSLayoutConstraint(item: new_view,
                attribute: .Leading,
                relatedBy: .Equal,
                toItem: view,
                attribute: .LeadingMargin,
                multiplier: 1.0,
                constant: 0.0).active = true
            NSLayoutConstraint(item: new_view,
                attribute: .Top,
                relatedBy: .Equal,
                toItem: view,
                attribute: .TopMargin,
                multiplier: 1.0,
                constant: 20.0).active = true
            NSLayoutConstraint(item: new_view,
                attribute: .Height,
                relatedBy: .Equal,
                toItem: new_view,
                attribute:.Width,
                multiplier: 2.0,
                constant:0.0).active = true
    
            NSLayoutConstraint(item: new_view,
                attribute: .Width,
                relatedBy: .Equal,
                toItem: nil,
                attribute: .NotAnAttribute,
                multiplier: 1.0,
                constant: 100.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: