UITextfiels界限,奇怪的行为

时间:2012-01-17 19:19:59

标签: ios uitextfield bounds

我不明白简单代码会发生什么。我想调整UITextField的宽度,但是当我这样做时,它也会更改其origin.x。我尝试了很多配置,但我找不到解决方案。

文本字段位于UIView

containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
containerView.backgroundColor = [UIColor whiteColor];

textfield = [[UITextField alloc] initWithFrame:CGRectMake(5,20,200,30)];
textfield.leftViewMode = UITextFieldViewModeAlways;

以下是更改宽度的代码:

[UIView beginAnimations:nil context:nil];

textfield.bounds = CGRectMake(textfield.bounds.origin.x,
                                  textfield.bounds.origin.y,
                                  textfield.bounds.size.width + 110, 
                                  textfield.bounds.size.height);
[UIView commitAnimations];

每次origin.x更改为-55(但NSLog打印0)。你有什么主意吗?我想始终保持相同的origin.x

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试更改框架,而不是更改边界:

[UIView beginAnimations:nil context:nil];

textfield.frame= CGRectMake(textfield.frame.origin.x,
                              textfield.frame.origin.y,
                              textfield.frame.size.width + 110, 
                              textfield.frame.size.height);
[UIView commitAnimations];

来自文档:

Changing the bounds size grows or shrinks the view relative to its center point

这似乎是你案件中的问题。