Ios UITextfiels界限,奇怪的行为

Ios UITextfiels界限,奇怪的行为,ios,uitextfield,bounds,Ios,Uitextfield,Bounds,我不明白一个简单的代码会发生什么。我想调整UITextField的宽度,但这样做时,它也会更改其origin.x。我尝试了许多配置,但找不到解决方案 文本字段位于ui视图中: containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)]; containerView.backgroundColor = [UIColor whiteColor]; textfield = [[UITextField alloc]

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

文本字段位于
ui视图中

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


感谢您的帮助。

请尝试更改帧,而不是更改边界:

[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
这似乎是你的问题所在