2012-01-17 27 views
0

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

的文本字段处于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

而是改变边界的,请尝试更改框架:

[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 

,这似乎是在你的情况的问题。

+0

完美!它的解决方案,谢谢你的帮助。 – user269568 2012-01-19 10:05:46