Reputation: 1751
I have created an app, in which i have a tableview
. I want to move the view
in certain conditions so I have changed the constraints, its working fine but when i tried to change the constraints again, the view
is not moved.
My code as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UINib *nib = [UINib nibWithNibName:@"GoalDetailsCustomCardCell" bundle:nil];
[goalDetailsTableView registerNib:nib forCellReuseIdentifier:@"CardCell"];
GoalDetailsTableViewCell *cell = [goalDetailsTableView dequeueReusableCellWithIdentifier:@"CardCell"];
if(cell == nil)
{
cell = [goalDetailsTableView dequeueReusableCellWithIdentifier:@"CardCell"];
}
if([self.swipedRowArray containsObject:indexPath])
{
NSLog(@"%f", cell.cardDetails.frame.origin.x);
NSLog(@"%f", cell.cardDetails.frame.size.width);
NSLog(@"%f", cell.cardDetails.frame.size.height);
cell.cardDetails.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *contentDictionary = @{@"cardDetails":cell.cardDetails, @"actionReminderCard":cell.actionCardReminder};
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(-314)-[cardDetails]" options:0 metrics:nil views:contentDictionary]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[actionReminderCard]" options:0 metrics:nil views:contentDictionary]];
NSLog(@"%f", cell.cardDetails.frame.size.width);
NSLog(@"%f", cell.cardDetails.frame.size.height);
}
else
{
NSLog(@"%f", cell.cardDetails.frame.origin.x);
NSLog(@"%f", cell.cardDetails.frame.size.width);
NSLog(@"%f", cell.cardDetails.frame.size.height);
cell.cardDetails.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *contentDictionary = @{@"cardDetails":cell.cardDetails, @"actionReminderCard":cell.actionCardReminder};
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[cardDetails]" options:0 metrics:nil views:contentDictionary]];
[cell.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-330-[actionReminderCard]" options:0 metrics:nil views:contentDictionary]];
NSLog(@"%f", cell.cardDetails.frame.size.width);
NSLog(@"%f", cell.cardDetails.frame.size.height);
}
In the above code, if part is working perfectly, when it comes to else i tried to change the constraints to default constraints as set in the xib
but it is not working. It shows the view as same as I changed the constraints before.
Thanks in advance.
Upvotes: 0
Views: 419
Reputation: 5164
Whenever you are adding constraint. it will not remove when you add new constraint. so you should delete those constraint and add new constraint or you should update same constraint. you can do this in two ways.
using programatically first remove all constraint and then add new constraint. same as @Tejvansh Singh Chhabra's answer. but make sure it will remove all your contentview's constraint.
[cell.contentView removeConstraints:cell.contentView.constraints];
The Other option is take outlet of NSLayoutConstraint
X position and then update it. but i think for that you should add all your contentview's subviews to UIView and then bind Leading constraint to your outlet and just change cell.yourConstraint.constant = yourvalue
in your if-else condition.
take one outlet in your customcell
IBOutlet NSLayoutConstraint *constraint_xPosition;
then outlet it as i shown in below image.
and in if-else condition change constant like cell.constraint_xPosition.constant = 0;//-320
Maybe this will help you.
Upvotes: 1
Reputation: 676
You need to first remove your old constraints before adding new constraints to cell maybe thats what causing issue. After removing all constraints you need to add all constraints again programmatically.
[cell.contentView removeConstraints:cell.contentView.constraints];
Upvotes: 1