Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITextField占位符对齐滚动故障_Ios_Objective C_Uitableview_Uitextfield_Constraints - Fatal编程技术网

Ios UITextField占位符对齐滚动故障

Ios UITextField占位符对齐滚动故障,ios,objective-c,uitableview,uitextfield,constraints,Ios,Objective C,Uitableview,Uitextfield,Constraints,我正在写一份登记表,实际上我遇到了一些麻烦 我正在单元格中使用UITextField,并在占位符中写入了一个字符串。在我尝试将UITextField.textAlignment居中之前,一切正常。不知怎的,它设法不把它放在中心。我检查了它并做了一些测试,我认为这可能是一个限制问题(我真的希望我的表单适用于iPhone4S、5、6和6+屏幕)。当我在iphone 6上时,它看起来几乎居中,但你会看到字符串越来越不居中,如果你切换到iphone 5,然后切换到iphone 4s,它会向右移动。。到目

我正在写一份登记表,实际上我遇到了一些麻烦

我正在单元格中使用UITextField,并在占位符中写入了一个字符串。在我尝试将UITextField.textAlignment居中之前,一切正常。不知怎的,它设法不把它放在中心。我检查了它并做了一些测试,我认为这可能是一个限制问题(我真的希望我的表单适用于iPhone4S、5、6和6+屏幕)。当我在iphone 6上时,它看起来几乎居中,但你会看到字符串越来越不居中,如果你切换到iphone 5,然后切换到iphone 4s,它会向右移动。。到目前为止,我只使用了两个高度约束

现在“好”的事情是当我滚动我的tableView时,如果我的单元格看不见了,一旦我重新加载单元格,占位符字符串第二次出现,但在正确的位置(这次是真正的中心),与旧的错误放置的占位符字符串。我想这个占位符显示当我第一次加载我的表时,我滚动出现,因为它似乎是我想要得到的确切结果。但这仅在我尝试将占位符居中时出现,如果不居中,则不会显示第二个占位符字符串。我会给你们两个图片链接,因为我的英语很糟糕,你们可以通过这种方式更好地理解

+=当我滚动tableView时,我的代码没有发生任何变化,它运行的是相同的CellForRowAtIndexPath,但结果仍然不同

谢谢你们给我的建议和帮助

占位符未完全居中的正常屏幕:

第二个占位符位于右侧的小车屏幕:

下面是我正在运行的代码:

- (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;
}

第一件事是,您正在cellForRowAtIndexPath方法中分配uitextfield,这就是为什么在滚动时它们会一次又一次出现的原因。因为uitableview的本质是在可见单元格中而不是在屏幕外执行开发人员希望执行的操作,这就是为什么在反复滚动此方法时调用此方法。只需尝试在viewDidLoad方法或其他地方分配textfield。 第二件事是使它们如您所愿可见,您可以在uitableviewcell中水平和垂直使用约束中心。要在UI中执行此操作,只需选择您的uitextfield,按住control键并将其拖动到该字段中,然后单击“居中水平”并垂直调用。
我希望这些建议能解决你的问题

谢谢你,你的第一个回答真的帮助我理解了我的错误。但我无法理解第二部分,我的uiTextField只存在于我的代码中,我没有将它加入到我的故事板中,所以我想我必须将约束修正到代码中?当然,你可以在代码中这样做,在代码中这样做比在ui中更好。因此,只需检查下面的代码如何做到这一点。我刚给了50分,你可以随意调整。yourtextfield.translatesAutoresizingMaskIntoConstraints=否;NSDictionary*viewsDictionary=NSDictionaryOfVariableBindings(yourtextfield);NSArray*constarints=[NSLayoutConstraint Constraints With VisualFormat:@“H:|-50-[yourtextfield]-50-|”选项:0度量值:无视图:viewsDictionary];[yourtextfield addConstraints:constraints];如果需要,也可以垂直进行。谢谢你,经过2小时的努力,我找到了修复这些约束的方法(因为它是self.LastName,我在将TextView放入单元格之前使用了constraits,我约束的是TextView,而不是单元格本身)。非常感谢你的帮助,多亏了你,我今天学到了更多关于iOs的知识。