简体   繁体   中英

UITextfield constraints in UITableViewCell

I have a UITableView with UITableViewCells that contain a UITextfield , but when I test it on other devices, it doesn't have any constraint. I want it to stick to the right of the UITableViewCell , how do I add this programmatically?

nameField.translatesAutoresizingMaskIntoConstraints = NO;

NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:nameField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
            [nameField addConstraint:constraint2];

            [cell.contentView addSubview:nameField];

I hope it's help you. // you can set UITextField alignment on right side

UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(120, 8, 185, 30)];
nameField.adjustsFontSizeToFitWidth = YES;

[nameField setFont:[UIFont fontWithName:@"Roboto-Regular" size:15]];

[nameField setUserInteractionEnabled:NO];
[nameField setDelegate:self];
[nameField setTextColor:[UIColor lightGrayColor]];

cell.textLabel.text = @"Server Name";
            [nameField setUserInteractionEnabled:YES];
            nameField.textAlignment=NSTextAlignmentRight;  // you can set right alignment
            nameField.text = [serverInfo valueForKey:@"name"];
            [textFields addObject:nameField];
            [cell.contentView addSubview:nameField];

Try using the following code (replacing my field with yours). I use it to center my text view but I changed the horizontal constraint so it right-justifies it instead.

     cell = [tableView dequeueReusableCellWithIdentifier:@"yourCustomCell"];
     [[cell.contentView subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
     UILabel *myLabel = [[UILabel alloc]init];
     myLabel.text = @"Detail";
     //Create your text view here
     [cell.contentView addSubview:myLabel];
     [cell.contentView addSubview:nameField];
     myLabel.translatesAutoresizingMaskIntoConstraints = NO;
     nameField.translatesAutoresizingMaskIntoConstraints = NO;
     NSMutableArray *result = [[NSMutableArray alloc] initWithCapacity:3];
     NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(myLabel, nameField);
     NSString *verForName = [NSString stringWithFormat:@"V:|[nameField]|"];
     NSString *verForLabel = [NSString stringWithFormat:@"V:|[myLabel]|"];
     NSString *horForView = [NSString stringWithFormat:@"H:|[myLabel]->=1-[nameField]-15-|"];
     [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:verForLabel options:0 metrics:nil views:viewsDictionary]];
     [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:verForName options:0 metrics:nil views:viewsDictionary]];
     [result addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:horForView options:0 metrics:nil views:viewsDictionary]];

     [cell.contentView addConstraints:result];      }

Try adding the following:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:nameField attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]
[textField addConstraint:constraint];

Disclaimer: Did this all in this text editor for the answer, so it may not be syntactically correct, but this is the general idea. If you need any help, just let me know!

You are missing nameField.translatesAutoresizingMaskIntoConstraints = NO; if you are doing autolayout.

And after that you just have to create the constrint:

NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:nameField 
                                                     attribute:NSLayoutAttributeRight 
                                                     relatedBy:NSLayoutRelationEqual 
                                                     toItem:cell attribute:NSLayoutAttributeRight 
                                                     multiplier:1.0 constant:0]
[namefield addConstraint:constraint];

and the constraints for the dimensions - height and width.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM