0

I am trying to customize the appearance of the the cells in the Table using the add constraints.

The current image of the code without adding constraints

I am trying to add constraints so that it gives an automatic placement for all scenarios. Below is the def for the texfield.

UITextField *txt = [[UITextField alloc] initWithFrame:CGRectMake(0, 7, 170, 22)];
txt.placeholder = @"Welcome";
txt.font = kFontSize16;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
txt.returnKeyType = UIReturnKeyNext;
txt.translatesAutoresizingMaskIntoConstraints = NO;
txt.delegate = self;
txt.textColor = kColorDeviceListStatusLabel;
txt.keyboardType = UIKeyboardTypeDecimalPad;
self.txthello = txt;

Below is the def of the UITableViewCell:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.font = myfont;

cell.textLabel.highlightedTextColor = [UIColor whiteColor];
cell.textLabel.text = @"From:";
cell.contentView addSubview:self.txthello];
NSLog(@"working till here");

[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtIPStart
                                                                     attribute:NSLayoutAttributeLeading
                                                                     relatedBy:NSLayoutRelationEqual
                                                                     toItem: cell.contentView
                                                                     attribute:NSLayoutAttributeLeft
                                                                     multiplier:1.0
                                                                    constant:90.0]];

NSLog(@" the log is not displayed here.");
[cell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.txtIPStart
                                                                     attribute:NSLayoutAttributeTrailing
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:cell.contentView
                                                                     attribute:NSLayoutAttributeRight
                                                                    multiplier:1.0
                                                                      constant:-50.0]];
return cell;

For some reason , when I simulate, it fails at the section toItem:cell.contentView

The error is Thread 1:signal abort not sure what is causing this.

Thanks in advance.

Update: Below is the stack crash.

0   tableSample                  0x0000000101f61bd7 -[SettingViewController layoutView] + 55
1   tableSample                  0x0000000101f6864c -[SettingViewController viewDidLoad] + 76
2   tableSample                  0x000000010200c80e __47+[UIViewController(Hooks) swizzle_viewDidLoad:]_block_invoke + 69
3   UIKit                               0x00000001030e3931 -[UIViewController loadViewIfRequired] + 1344
4   UIKit                               0x0000000103126c26 -[UINavigationController _layoutViewController:] + 54
5   UIKit                               0x00000001031274dd -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 433
6   UIKit                               0x0000000103127633 -[UINavigationController _startTransition:fromViewController:toViewController:] + 116
7   UIKit                               0x0000000103128879 -[UINavigationController _startDeferredTransitionIfNeeded:] + 890
8   UIKit                               0x000000010312967d -[UINavigationController __viewWillLayoutSubviews] + 57
9   UIKit                               0x00000001032c163d -[UILayoutContainerView layoutSubviews] + 248
10  UIKit                               0x000000010300911c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
11  QuartzCore                          0x000000010276036a -[CALayer layoutSublayers] + 146
12  QuartzCore                          0x0000000102754bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
13  QuartzCore                          0x0000000102754a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
14  QuartzCore                          0x00000001027491d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
15  QuartzCore                          0x00000001027769f0 _ZN2CA11Transaction6commitEv + 508
16  UIKit                               0x0000000102f8253a _afterCACommitHandler + 174
17  CoreFoundation                      0x0000000104fa79d7 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
18  CoreFoundation                      0x0000000104fa7947 __CFRunLoopDoObservers + 391
19  CoreFoundation                      0x0000000104f9d59b __CFRunLoopRun + 1147
20  CoreFoundation                      0x0000000104f9ce98 CFRunLoopRunSpecific + 488
21  GraphicsServices                    0x0000000106196ad2 GSEventRunModal + 161
22  UIKit                               0x0000000102f58676 UIApplicationMain + 171
23  tableSample                  0x0000000101fba91f main + 111
24  libdyld.dylib                       0x000000010584192d start + 1

Update 2: Hello... Caught exception *** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A constraint cannot be made between a leading/trailing attribute and a right/left attribute. Use leading/trailing for both or neither.

dan
  • 9,695
  • 1
  • 42
  • 40
b0b
  • 33
  • 5
  • The error message is pretty clear: Change your first constraint so that both attributes are `NSLayoutAttributeLeading` and the second one so both attributes are `NSLayoutAttributeTrailing` – dan Apr 11 '16 at 23:33
  • Yes. Thank you. For this to work, the attribute constraint must be the same. – b0b Apr 14 '16 at 21:41

1 Answers1

0

You should set setTranslatesAutoresizingMaskIntoConstraints to NO when set constraint from code.

[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];

Hope this will help you. :)

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75