Updating NSLayoutConstraints after first table view

  • Loading custom UITableViewCell

    from xib
  • I update NSLayoutConstraint

    programmatically in cellForRowAtIndexPath

Auto shutdown and manual change NSLayoutConstraint

works as it should - device size, orientation, etc.

BUT: in the very FIRST view of the cell, program changes are not displayed.
Changing views and returning updates as expected.

I believe I need to deliver [self setNeedsUpdateConstraints]

somewhere.
I've tried all sorts of places ( viewDidAppear, didEndDisplayingCell ...

)

Where should it be?

Could this be a new cell / reusable cell issue?

+3


source to share


4 answers


You've already updated (changed) the constraint, so the view doesn't need to update your constraints. This requires updating your layout in response to the constraint change. So, I think you need to do [view setNeedsLayout]

. This must be done in the view to which the constraint was added (or in the common ancestor view of all views associated with the constraint change). This must be done in the same code that changes the constraint, immediately after changing it.



However, I'm not sure what -tableView:cellForRowAtIndexPath:

is a good place to try changing the constraint on the returned view. At this point, the cell view has not been added as a table lookup. Constraints can only be added to views in the same view hierarchy.

0


source


So to summarize - this is what I did to get nicely laid out looks every time:

UIView is now responsible for itself, no messing around with the tableviewController.

I didn't need to call (void) updateConstraints anywhere, which makes sense as I was setting constraints in IB and just adding a property to be able to change the constant. The actual constraints were left intact and are essential for the layout!



  • Moved all layout code into my UITableViewCell subclass, named it (void) updateMedian (just two lines of code).
  • Created a property to pass the information (base float) needed to recalculate the position of the label to the subclass.
  • Used by (void) updateMedian in (void) layoutSubviews subclass UITableViewCell to update my coordinates.
  • Also added a call to (void) updateMedian and [self layoutIfNeeded] to (void) prepareForReuse in my subclass.
  • Added [cell setNeedsLayout]; [cell layoutIfNeeded] in cellForRowAtIndexPath in my tableviewController.

I think the last step is a little redundant. But otherwise, there were still some cells that did not display correctly.

+2


source


Subclass UITableViewCell

and call setNeedsLayout

in awakeFromNib:

.

You can also carry the update constraints onto the cell method updateConstraints

, making yours a UITableViewController

little smaller. After all, the cell is separate UIView

and it needs to know how the layout itself, not the table view controller.

0


source


From your question what I am getting like

If you can provide constraints for an object from the storyboard using pin, then it will set those constraints for the object and if you added the constraint programmatically in "cellForRowAtIndexPath:" it will be overly rights that you set in the storyboard. You could only set them for an individual cell by mistake, so that it can show the effect in a separate cell.

You can try the next solution and see if it helps you. Place this code in the cellForRow method.

Ive implemented

NSDictionary *viewsDictionary = @{@"redView":cell.lblText};
    NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[redView(10)]"
                                                                    options:0
                                                                    metrics:nil
                                                                      views:viewsDictionary];

    NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[redView(100)]"
                                                                    options:0
                                                                   metrics:nil
                                                                   views:viewsDictionary];

    [cell addConstraints:constraint_H];
    [cell addConstraints:constraint_V];

      

in cellForRowAtIndexPath:

and it touched all the rows in the cell and it moved the constraints.

You can also visit these links for further links: http://technet.weblineindia.com/mobile/ui-design-of-ios-apps-with-autolayout-using-constraints-programmatically/ http: //www.thinkandbuild. it / learn-to-love-auto-layout-programmatically /

0


source







All Articles