UITextField使用自动布局在UITableViewCell中

时间:2013-11-11 02:48:05

标签: objective-c uitableview ios7 autolayout

我有UITableViewController,显示一个部分和一个单元格(测试用例)。 在该单元格内,我有一个UITextField,它应该跨越单元格contentView的长度,两边各有20个点(即H:|-20-[field]-20-|)并且垂直居中。 当我使用具有必要约束的笔尖时,它可以完美地工作,但是当我以编程方式添加约束时,它似乎没有遵循它们。 (见图片。)

NSLog()中的-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;
}

这是我在不使用笔尖时所得到的(在横向上没有区别): Programmatic auto layout

这就是我用笔尖得到的东西(适合横向缩放): IB auto layout

1 个答案:

答案 0 :(得分:1)

你应该删除这一行,它会正常工作:

cell.contentView.translatesAutoresizingMaskIntoConstraints = NO;

您只需将自己添加的子视图设置为NO即可。

另外,我注意到你在cellForRowAtIndexPath中注册了nib(如果你正在使用nib) - 你不应该这样做,因为cellForRowAtIndexPath被多次调用。您应该将该代码放在viewDidLoad中。

另一件事 - 你应该简化你的约束代码。使用可视化格式语言的最大优点是您可以在同一语句中设置多个约束,因此您可以将4个语句组合为2:

cell.contentView addConstraints:[NSLayoutConstraint
                                          constraintsWithVisualFormat:@"V:|-7-[field]-6-|"
                                          options:0
                                          metrics:nil
                                          views:objs]];

cell.contentView addConstraints:[NSLayoutConstraint
                                          constraintsWithVisualFormat:@"H:|-20-[field]-20-|"
                                          options:0
                                          metrics:nil
                                          views:objs]];