Search code examples
iosobjective-cnslayoutconstraintuitableview

how to access NSLayoutConstraint into tableviewcell with identifier to it?


I am using autolayout into my project and there is one situation in which I stuck. I already done with it Custom XIB of TableviewCell but I want to do it with default prototype cell of Tableview. My design is like below

design

Now In my design I select constraint top to button and giving identifier like below

constraint

In Tableview Prototype cell, I cant give outlet of it. So how can i access that NSLayoutConstraint with identifier. I search through web but nothing found

Please help me.
Thank you


Solution

  • Hope this helps.

        NSArray *constraints = [myView constraints]; // Here myView is a reference to the required UI component 
        int count = [constraints count];
        int index = 0;
        BOOL found = NO;
    
        while (!found && index < count) {
            NSLayoutConstraint *constraint = constraints[index];
            if ( [constraint.identifier isEqualToString:@"topconstraint"] ) {
                //save the reference to constraint
                constraint.constant = yourValue;
                found = YES;
            }
            index++;
        }
    

    Using Predicate also, one can filter out:

    constraints.filter{ $0.identifier == "MyIdentifierText" }.first { }