0

I ve been using storyboards so far to set my constraints but I am attempting to learn how to do things programmatically. The issue at hand:

A table needs to return a cell in cellForRowAt. There I simply need to add a UILabel element that will be constraint to the top left bottom and right anchors. The table cell height is set to automatic as I don't know what the size of the label will be. My code looks as follows:

var uil = UILabel()

cell.addSubview(uil)
uil.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true
uil.rightAnchor.constraint(equalTo: cell.rightAnchor).isActive = true
uil.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
uil.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
uil.numberOfLines = 0;
uil.text = "Some variable sized text that could be anything really";

If I don't give a frame to the UILabel I see nothing. If I do however using something like:

UILabel(frame: CGRect(x: 0 , y: 0 , width: 100, height: 100)

Then as expected I see the label but it's like the constraints don't apply.

What am I missing? Shouldn't the constraints be enough as they are fully descriptive?

4 Answers 4

1

If you are dynamically adding views then you need to set view.translatesAutoresizingMaskIntoConstraints = false

For more details click here

and then apply below constraints as It will work.

// align lbl from the left and right
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[view]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": lbl]));

// align lbl from the top and bottom
cell.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[view]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view": lbl]));
Sign up to request clarification or add additional context in comments.

Comments

1

I think you should add this line before applying constraints

uil.translatesAutoresizingMaskIntoConstraints = false

Comments

0

Heres a working code snippet for you i have done it in view controller you can do the same for a cell

 var uil:UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.uil = UILabel()
        self.uil?.translatesAutoresizingMaskIntoConstraints = false
        self.uil?.backgroundColor = UIColor.red
        self.uil?.text = "label text"
        self.uil.addSubview(self.uil!)
        self.uil?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant:10).isActive = true
        self.uil?.topAnchor.constraint(equalTo: view.topAnchor, constant:50).isActive = true
        self.uil?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant:-10).isActive = true
        self.uil?.heightAnchor.constraint(equalToConstant: 20.0).isActive = true


    }

Comments

0

Here is an example of how we can apply constraints. i will take a label as an example.

UILabel *lb1;
lb1 =[[UILabel alloc]init];
self.view addSubview:lb1];

lb1.translatesAutoresizingMaskIntoConstraints = NO;

// horizontal constraint

NSLayoutConstraint *centreHorizontallyConstraint = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];

//vertical constraint

NSLayoutConstraint *centreVeryConstraint = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];

//height constraint

NSLayoutConstraint * lblheight = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:30];

//width constraint

NSLayoutConstraint * lblwidth = [NSLayoutConstraint constraintWithItem:lb1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80];

//add constraint

[self.view addConstraints:@[centreHorizontallyConstraint,centreVeryConstraint,lblheight,lblwidth]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.