UITextField占位符对齐滚动麻烦

问题描述 投票:-1回答:1

我实际上正在处理注册表,并且遇到了一些麻烦。

我在单元格中使用了UITextField,并在占位符中写入了一个字符串。一切运行良好,直到我尝试将UITextField.textAlignment居中。它以某种方式设法不居中。我检查了一下并进行了一些测试,我认为这可能是约束问题(我真的希望我的表格适用于iPhone 4s,5、6和6+屏幕)。当我在iphone 6上时,它看起来几乎居中,但是随后您看到的字符串越来越不居中,如果切换到iphone 5和iphone 4s,则显示在右侧。.到目前为止,我只使用了两个高度约束。

现在,“好”是当我滚动tableView时,如果我的单元格不可见,则一旦我重新加载单元格,占位符字符串将再次出现,但在正确的位置(这次是真正的中心)旧的放错位置的占位符字符串。我希望此占位符显示在我第一次加载表格时滚动时出现,因为它似乎是我想要获得的确切结果。但这仅在我尝试将占位符居中时出现,如果不居中,则不会显示第二个占位符字符串。我会给你2张图片的链接,因为我的英语太糟糕了,所以你们也许可以这样更好地理解。

+ =当我滚动tableView时,我的代码没有任何反应,它正在运行相同的CellForRowAtIndexPath,但结果仍然不同。

谢谢您提供的所有建议和帮助,你们会带给我

带有占位符的普通屏幕未完全位于中心:

Buggy屏幕,第二个占位符位于正确位置:

这是我的代码正在运行:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSString *identifier = @"id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (indexPath.section == 0){
        self.LastName = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
        self.LastName.placeholder= @"Last Name";
        self.LastName.autocorrectionType = UITextAutocorrectionTypeNo;
        self.LastName.textAlignment = NSTextAlignmentCenter;
        self.LastName.delegate = self;
        [self.LastName setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.LastName setTextColor:[UIColor whiteColor]];

        [cell.contentView addSubview:self.LastName];
        [cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    else if (indexPath.section == 1)
    {
        self.Name = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
        self.Name.placeholder= @"Name";
        self.Name.autocorrectionType = UITextAutocorrectionTypeNo;
        self.Name.textAlignment = NSTextAlignmentCenter;
        self.Name.delegate = self;
        [self.Name setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.Name setTextColor:[UIColor whiteColor]];

        [cell.contentView addSubview:self.Name];
        [cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    else if (indexPath.section == 2)
    {
        self.Email = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
        self.Email.placeholder= @"Email";
        self.Email.autocorrectionType = UITextAutocorrectionTypeNo;
        self.Email.textAlignment = NSTextAlignmentCenter;
        self.Email.delegate = self;
        [self.Email setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.Email setTextColor:[UIColor whiteColor]];

        [cell.contentView addSubview:self.Email];
        [cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    else if (indexPath.section == 3)
    {
        self.Password = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
        self.Password.placeholder= @"Password";
        self.Password.autocorrectionType = UITextAutocorrectionTypeNo;
        self.Password.textAlignment = NSTextAlignmentCenter;
        self.Password.delegate = self;
        [self.Password setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.Password setTextColor:[UIColor whiteColor]];

        [cell.contentView addSubview:self.Password];
        [cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    else if (indexPath.section == 4)
    {
        self.RepetePassword = [[UITextField alloc] initWithFrame:cell.contentView.bounds];
        self.RepetePassword.placeholder= @"Repeat password";
        self.RepetePassword.autocorrectionType = UITextAutocorrectionTypeNo;
        self.RepetePassword.textAlignment = NSTextAlignmentCenter;
        self.RepetePassword.delegate = self;
        [self.RepetePassword setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
        [self.RepetePassword setTextColor:[UIColor whiteColor]];

        [cell.contentView addSubview:self.RepetePassword];
        [cell.contentView.layer setBorderColor:[UIColor whiteColor].CGColor];
    }
    else if (indexPath.section == 5)
    {
        cell.textLabel.text = @"Register";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.textColor = [UIColor whiteColor];
        [cell.contentView.layer setBackgroundColor:[UIColor greenColor].CGColor];
        [cell.contentView.layer setBorderColor:[UIColor clearColor].CGColor];
    }

    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView = [UIView new];
    cell.selectedBackgroundView = [UIView new];

    [cell.contentView.layer setBorderWidth:1.0f];
    [cell.contentView.layer setCornerRadius:20.0f];
    [cell.contentView.layer setMasksToBounds:YES];
    return cell;
}
ios objective-c uitableview uitextfield constraints
1个回答
0
投票

[第一件事是您在cellForRowAtIndexPath方法中分配了uitextfield,这就是为什么在滚动时它们会一次又一次出现的原因。因为uitableview的本质是开发人员想要在可见单元格中进行任何操作,而不是在屏幕外进行操作,因此滚动时此方法一次又一次地调用。只需尝试在viewDidLoad方法或其他位置分配文本字段即可。第二件事是使它们可见,您可以只在uitableviewcell中水平和垂直使用约束中心。要在UI中做到这一点,只需选择您的uitextfield并按住Control键并将其拖动到其中,然后单击居中水平调用并垂直单击即可。我希望这些建议可以解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.