我正在尝试使用添加约束来自定义表格中单元格的外观。
The current image of the code without adding constraints
我正在尝试添加约束,以便为所有方案自动放置。 下面是texfield的def。
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;
以下是UITableViewCell的def:
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;
出于某种原因,当我模拟时,它在toItem:cell.contentView
部分失败了错误是线程1:信号中止 不确定是什么导致了这一点。
提前致谢。
更新: 下面是堆栈崩溃。
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
更新2: Hello ...捕获异常*** + [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:不能在前导/尾随属性和右/左属性之间建立约束。对两者使用前导/尾随或两者都不使用。
答案 0 :(得分:0)
从代码设置约束时,您应该将setTranslatesAutoresizingMaskIntoConstraints
设置为NO
。
[centerView setTranslatesAutoresizingMaskIntoConstraints:NO];
希望这会对你有所帮助。 :)