【问题标题】:UITextField Won't Change Frame on RefocusUITextField 不会在重新聚焦时改变框架
【发布时间】:2012-11-04 19:19:57
【问题描述】:

我遇到了一个特殊的问题。我有两个 UITextFields 的视图,它们以 280px 宽开始。在焦点上,我希望它们缩短以显示一个按钮 - 我正在使用以下代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect revealButton = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 221, textField.frame.size.height);

    [UIView beginAnimations:nil context:nil];
    textField.frame = revealButton;
    [UIView commitAnimations];
    NSLog(@"%f",textField.frame.size.width);
}

编辑结束后,他们应该回到原来的帧:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect hideButton = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 280, textField.frame.size.height);

    [UIView beginAnimations:nil context:nil];
    textField.frame = hideButton;
    [UIView commitAnimations];
}

我第一次关注文本字段时,它工作得很好。但是,如果我在关注其他内容之后关注第一个文本字段(例如,如果我最初关注第一个文本字段,关注第二个,然后重新关注第一个,或者如果我最初关注第二个然后关注第一个),它根本不会改变它的框架。更令人费解的是,它记录 221 作为其宽度 - 它只是不会在屏幕上显示。此外,此问题不适用于第二个文本字段。

有什么想法吗?提前谢谢...

【问题讨论】:

    标签: iphone objective-c ios uitextfield frame


    【解决方案1】:

    这很奇怪,我使用两个具有完全相同代码的文本字段进行了快速测试,并且每次都有效。

    我建议删除文本字段和连接并重建它们。清理所有目标并重试。

    根据您的 cmets 进行编辑

    如果您使用自动布局,则不得直接修改文本字段的框架。 UI元素的实际帧数由系统计算。

    出于您的目的,我建议为每个文本字段设置宽度限制。确保除了宽度约束之外,您只有一个左 右间距约束,而不是两者。要对其进行动画处理,请使用以下代码:

    - (NSLayoutConstraint *)widthConstraintForView:(UIView *)view
    {
        NSLayoutConstraint *widthConstraint = nil;
    
        for (NSLayoutConstraint *constraint in textField.constraints)
        {
            if (constraint.firstAttribute == NSLayoutAttributeWidth)
                widthConstraint = constraint;
        }
    
        return widthConstraint;
    }
    
    - (void)animateConstraint:(NSLayoutConstraint *)constraint toNewConstant:(float)newConstant withDuration:(float)duration
    {
        [self.view layoutIfNeeded];
        [UIView animateWithDuration:duration animations:^{
            constraint.constant = newConstant;
            [self.view layoutIfNeeded];
        }];
    }
    
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        float newWidth = 221.0f;
    
        NSLayoutConstraint *widthConstraint = [self widthConstraintForView:textField];
    
        [self animateConstraint:widthConstraint toNewConstant:newWidth withDuration:0.5f];
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
        float newWidth = 280.0f;
    
        NSLayoutConstraint *widthConstraint = [self widthConstraintForView:textField];
    
        [self animateConstraint:widthConstraint toNewConstant:newWidth withDuration:0.5f];
    }
    

    【讨论】:

    • 是的 - 问题是,这在我启用自动布局之前就可以工作了......起初我认为这是约束的问题,但如果它是第一个文本字段,则它可以完美地工作一个要专注...
    • 如果您使用自动布局,则无法通过更改其框架来更改文本字段的大小。例如,您设置了宽度约束并对其进行修改。
    • 是的,这很奇怪,但由于在自动布局下,框架是由系统根据设置约束计算的,因此您无法手动修改它们(如果它有时可以工作,那似乎是一个错误)。无论如何,事实是要在使用自动布局时修改任何布局,您必须修改适当的约束。
    • 这确实有效 - 谢谢...有什么方法可以制作动画吗?
    • 我会更新我的答案,在评论中写这个有点困难
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多