2015-03-03 35 views
0

當我按下按鈕時,文本框的位置應該改變[例如:(0,0)到(50,50)]。該代碼工作完美。但是,如果我嘗試輸入文本框它回到同一幀的位置[0,0]如何更改文本字段的x和y位置以及編輯何時開始回到正常位置

,這是我的代碼

(IBAction)Buttonname:(id)sender 
{ 
CGRect newframeTextfield = textfiledname.frame; 
newframeTextfield.origin.y = 365; 
textfiledname.frame = newframeTextfield; 
} 

這是以前的位置

-(void)viewWillAppear:(BOOL)animated 
{ 
    CGRect oldTextfieldframe = textfiledname.frame; 
    oldTextfieldframe.origin.y = 333; 
    oldTextfieldframe.origin.x = 50; 
    textfiledname.frame = oldTextfieldframe; 
} 

和我當鍵盤出現時,我也使用scrollview和自動文本字段向上移動。

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
     if (textField == textfiledname) 
     { 
      [Scrolview setContentOffset:CGPointMake(0, 210) animated:YES]; 
      Scrolview.contentSize = CGSizeMake(320, 900); // there is submit button in bottom so scroll up to submit when keyboard appear. Otherwise (320,568) using in other function 
     } 
} 

和先感謝... :)

+0

中的「解決自動佈局問題」按鈕訪問。不清楚。你能解釋一下你的問題嗎? – 2015-03-03 06:34:33

+3

這是因爲自動佈局。你不應該通過設置框架來改變位置,你應該修改約束。任何會導致視圖重繪的東西(如在文本框中輸入)將導致視圖移回到由其約束確定的位置。 – rdelmar 2015-03-03 06:38:52

+0

@rdelmar非常感謝你.... :) – user3589771 2015-03-03 13:51:39

回答

0

這是由汽車造成的佈局被啓用。如果您不擅長使用自動佈局,請在IB中禁用「使用自動佈局」和「使用大小班級」選項。 如果您想繼續使用自動佈局,您可以嘗試添加缺少的約束。該選項可通過IB

相關問題