2012-01-17 45 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