我有一个UITableViewController
显示一个部分和一个单元格(测试用例)。在那个单元格中,我有一个UITextField
应该跨越单元格的长度,contentView
两边各有 20 个点(即H:|-20-[field]-20-|
)并垂直居中。当我使用带有必要约束的笔尖时,它可以完美地工作,但是当我以编程方式添加约束时,它似乎没有遵循它们。(见图片。)
对于这两种情况,打印中的NSLog()
's-tableView:willDisplayCell:forRowAtIndexPath:
约束完全相同。
FOOAppDelegate.m:
#import "FOOAppDelegate.h"
#import "FOORootViewController.h"
@implementation FOOAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:[[FOORootViewController alloc] initWithStyle:UITableViewStyleGrouped]];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
return YES;
}
@end
FOORootViewController.m:(注意:被声明为 UITextFieldDelegate)
#import "FOORootViewController.h"
enum { kFOOTextFieldTag = 0xFEED /* Arbitrary. */ };
@implementation FOORootViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
self.title = @"Foo";
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
return self;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FooCell";
/* Uncomment the following line when using nib. */
//[self.tableView registerNib:[UINib nibWithNibName:@"FooCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:CellIdentifier];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
/* Comment this if block when using nib. */
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
/* Comment this if block when using nib. */
if (self.editing && ([cell.contentView viewWithTag:kFOOTextFieldTag] == nil)) {
UITextField *field = [[UITextField alloc] init];
field.autocapitalizationType = UITextAutocapitalizationTypeSentences;
field.borderStyle = UITextBorderStyleLine;
field.delegate = self;
field.placeholder = @"New";
field.tag = kFOOTextFieldTag;
field.translatesAutoresizingMaskIntoConstraints = NO;
field.userInteractionEnabled = YES;
[cell.contentView addSubview:field];
NSDictionary *objs = NSDictionaryOfVariableBindings(cell.contentView, field);
cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
[cell.contentView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:|-7-[field]"
options:0
metrics:nil
views:objs]];
[cell.contentView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"V:[field]-6-|"
options:0
metrics:nil
views:objs]];
[cell.contentView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:|-20-[field]"
options:0
metrics:nil
views:objs]];
[cell.contentView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:@"H:[field]-20-|"
options:0
metrics:nil
views:objs]];
}
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UITextField *field = (UITextField *)[cell.contentView viewWithTag:kFOOTextFieldTag];
if (self.editing) {
field.hidden = NO;
NSLog(@"field: %@", field);
} else {
field.hidden = YES;
}
NSLog(@"cell.contentView: %@", cell.contentView);
NSLog(@"constraints: %@", cell.contentView.constraints);
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleInsert;
}
这是我在不使用笔尖时得到的(横向时没有区别):
这就是我用笔尖得到的(适合横向缩放):