Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8761661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:20:27+00:00 2026-06-13T15:20:27+00:00

I would like to use auto layout to position a UITextField next to cell.textLabel

  • 0

I would like to use auto layout to position a UITextField next to cell.textLabel when a UITableView goes into edit mode. The code that I have works correctly but I get a message in the log that says some existing constraints had to be broken.

UILabel *label = cell.textLabel;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 22.0)];
textField.placeholder = cell.textLabel.text;
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.text = text;
[cell.contentView addSubview:textField];
[cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label]-[textField]-|"
                                                             options:0
                                                             metrics:nil
                                                               views:NSDictionaryOfVariableBindings(label,textField)]];
[cell addConstraint:[NSLayoutConstraint constraintWithItem:textField attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:label attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];

Log message is:

(
"<NSAutoresizingMaskLayoutConstraint:0x1066abc0 h=--& v=--& H:[UITableViewCellContentView:0x9a7d070(934)]>",
"<NSAutoresizingMaskLayoutConstraint:0x10660a90 h=--& v=--& H:[UILabel:0x9a93ef0(914)]>",
"<NSLayoutConstraint:0x1065ea60 H:[UITextField:0x1065c660]-(NSSpace(20))-|   (Names: '|':UITableViewCellContentView:0x9a7d070 )>",
"<NSAutoresizingMaskLayoutConstraint:0x10660a50 h=--& v=--& UILabel:0x9a93ef0.midX == + 467>",
"<NSLayoutConstraint:0x10669280 H:[UILabel:0x9a93ef0]-(NSSpace(8))-[UITextField:0x1065c660]>"

)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x10669280 H:[UILabel:0x9a93ef0]-(NSSpace(8))-[UITextField:0x1065c660]>

I think that this is caused by conflicting restraints with the cell.textLabel width. So my question is if there is another way to have a textfield in a standard table view cell that stretches from the textLabel width to the end of the cell without breaking default constraints. I feel like I’m close but I can’t quite get there. I’ve searched Google over for three weeks but can’t quite get my head around this. I’ve also watched WWDC videos on auto layout (perhaps I’m just an idiot). Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:20:28+00:00Added an answer on June 13, 2026 at 3:20 pm

    Instead of trying to manipulate Apple’s standard cell, I took the plunge and wrote my own UITableViewCell subclass that mimics UITableViewCellStyleValue1. When the tableview goes into edit mode, in the simplest terms I hide the value label and display the textfield. For those who might be struggling with the same thing, I’m posting some code to help you get started:

    @interface NXAlphaNumericTextFieldCell : UITableViewCell<UITextFieldDelegate,NumberKeyboardDelegate>
    
    @property (strong, nonatomic)   UITextField *inputTextField;
    @property (strong, nonatomic)   UILabel *titleLabel;
    @property (strong, nonatomic)   UILabel *valueLabel;
    
    @property (strong, nonatomic)   NSArray *xTitleLabelConstraints;
    @property (strong, nonatomic)   NSArray *xTextFieldConstraints;
    @property (strong, nonatomic)   NSArray *xValueLabelConstraints;
    
    @end
    

    And a few methods in the implementation:

    - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
    {
        self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
        if (self) {
            // Initialization code
            self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)];
            self.titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
            self.titleLabel.font = [UIFont boldSystemFontOfSize:16.0f];
            self.titleLabel.backgroundColor = [UIColor clearColor];
    
            self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 44.0f)];
            self.valueLabel.translatesAutoresizingMaskIntoConstraints = NO;
            self.valueLabel.textColor = [UIColor colorWithRed:81.0/255.0 green:102.0/255.0 blue:145.0/255.0 alpha:1.0];
            self.valueLabel.backgroundColor = [UIColor clearColor];
    
            self.inputTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 44.0f)];
            self.inputTextField.translatesAutoresizingMaskIntoConstraints = NO;
            self.inputTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;
            self.inputTextField.autocorrectionType = UITextAutocorrectionTypeYes;
            self.inputTextField.clearButtonMode = UITextFieldViewModeAlways;
            self.inputTextField.delegate = self;
    
            [self.contentView addSubview:self.valueLabel];
            [self.contentView addSubview:self.titleLabel];
            [self.contentView addSubview:self.inputTextField];
    
            UILabel *textLabel = self.titleLabel;
            NSDictionary *labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(textLabel);
            self.xTitleLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[textLabel]"
                                                                                 options:0
                                                                                 metrics:nil
                                                                                   views:labelTextFieldViewsDictionary];
            UITextField *textfield = self.inputTextField;
            labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(textLabel, textfield);
            self.xTextFieldConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[textLabel]-50-[textfield]-|"
                                                                                     options:0
                                                                                     metrics:nil
                                                                                       views:labelTextFieldViewsDictionary];
            UILabel *valueLabel = self.valueLabel;
            labelTextFieldViewsDictionary = NSDictionaryOfVariableBindings(valueLabel);
            self.xValueLabelConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[valueLabel]-|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:labelTextFieldViewsDictionary];
    
            [self setNeedsUpdateConstraints];
        }
        return self;
    }
    
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated
    {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
        if (self.isEditing) {
            [self.inputTextField becomeFirstResponder];
        }
    }
    
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    {
        [super setEditing:editing animated:animated];
    
        [self addConstraints:self.xTitleLabelConstraints];
        if (editing) {
            if (self.inputType == kCellInputTypeAlphaNumeric) {
                self.inputTextField.keyboardType = UIKeyboardTypeAlphabet;
            } else if (self.inputType == kCellInputTypeEmail) {
                self.inputTextField.keyboardType = UIKeyboardTypeEmailAddress;
            } else if (self.inputType == kCellInputTypePhoneNumber) {
                self.inputTextField.keyboardType = UIKeyboardTypeNamePhonePad;
            } else {
                if (!self.numberKeyboard) {
                    self.numberKeyboard = [[NumberKeyboard alloc] initWithNibName:@"NumberKeyboard" bundle:nil];
                    self.numberKeyboard.textField = self.inputTextField;
                    self.numberKeyboard.showsPeriod = YES;
                    self.numberKeyboard.delegate = self;
                }
                self.inputTextField.inputView = self.numberKeyboard.view;
            }
            self.inputTextField.text = self.valueLabel.text;
            self.inputTextField.placeholder = self.titleLabel.text;
            self.valueLabel.hidden = YES;
            self.inputTextField.hidden = NO;
    
            [self removeConstraints:self.xValueLabelConstraints];
            [self addConstraints:self.xTextFieldConstraints];
        } else {
            [self.inputTextField resignFirstResponder];
            self.inputTextField.hidden = YES;
            self.valueLabel.hidden = NO;
            [self removeConstraints:self.xTextFieldConstraints];
            [self addConstraints:self.xValueLabelConstraints];
        }
    }
    
    - (void)updateConstraints
    {
        [super updateConstraints];
    
        if (self.editing) {
            [self removeConstraints:self.xValueLabelConstraints];
            [self addConstraints:self.xTextFieldConstraints];
        } else {
            [self removeConstraints:self.xTextFieldConstraints];
            [self addConstraints:self.xValueLabelConstraints];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing a javascript function in Eclipse, I would like to use the auto-commenting
I cant use auto increment in phpmyadmin for this but I would like to
For duplicating an entry I would like to use the following syntax: insert into
I have an idea for a layout I would like to use, but I
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted
I would like to use Maven's password encryption such as it uses for nodes
I would like to use the logout function from Django but not sure how
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using
I would like to use the jenkins script console some more. Where do I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.