2013-12-11 28 views
0

我在iOS中有一個UITextfield和UITextField下面有3個UIbutton的視圖。請參閱下面的圖片。啓動此視圖時,默認狀態爲STATE1(請參閱圖像)。鍵盤默認是可見的。現在當我處理鍵盤時,我希望editatetext被調整並佔據整個屏幕,如STATE2所示。iOS-基於鍵盤顯示的UITextField高度和按鈕移動的更改

我不知道如何做到這一點。我將UITextfield的高度硬編碼爲基於目標設備的某個dp。我相信這必須改變,它必須根據屏幕尺寸動態佔據屏幕。 enter image description here

任何人都可以幫助我做到這一點。請考慮按鈕就像編輯文本的尾部。無論鍵盤是否可見,這都貼在編輯文本上。謝謝

回答

0

你可以使用TPKeyboardAvoiding庫,並實現這個你想要的。

+0

閱讀它。會回到你身邊。謝謝 –

+0

如果它對你的錯誤,然後請求+1。謝謝。 –

0

首先,你必須使用的UITextView代替的UITextField

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

的註冊類實現此方法

- (void)keyboardWillHide:(NSNotification *)notification { 
    [self resizeViewWithOptions:[notification userInfo]]; 
} 

- (void)keyboardWillShow:(NSNotification *)notification { 
    [self resizeViewWithOptions:[notification userInfo]]; 
} 

- (void)resizeViewWithOptions:(NSDictionary *)options { 

    NSTimeInterval animationDuration; 
    UIViewAnimationCurve animationCurve; 
    CGRect keyboardEndFrame; 
    [[options objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; 
    [[options objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; 
    [[options objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationCurve:animationCurve]; 
    [UIView setAnimationDuration:animationDuration]; 
    CGRect viewFrame = self.view.frame; 

    CGRect keyboardFrameEndRelative = [self.view convertRect:keyboardEndFrame fromView:nil]; 
    NSLog(@"keyboardFrameEndRelative: %@", NSStringFromCGRect(keyboardFrameEndRelative)); 

// Now set Frame for UITextView and UIbuttons frame here 

} 
+0

嗨Bhuvaneshwar,我們需要在哪裏註冊該課程。你能不能讓我知道。這裏addObserver是什麼?謝謝 –

+0

@TimothyRajan在你的課堂上,你必須寫下這個方法。在'viewdidLoad'中爲你的類註冊鍵盤通知,並在同一個類中定義這個方法。這裏'addObserver'表現爲一個目標。 –

0

嘗試這樣就更簡單了第一個加法那些控制你想要的狀態,如狀態1

在viewDidAppear添加的功能

-(void)viewDidAppear:(BOOL)animated 
{ 
    [self.Textfield1 becomeFirstResponder]; 

} 

所以它會加載鍵盤當視圖加載

然後添加此行.M關閉鍵盤按返回它會隱藏鍵盤

-(BOOL) textFieldShouldReturn:(UITextField *)textField{ 

    //Write your coding for resizing size dynamically to show look like state 2 
    //Here check version of device to set as per height 
    textfield.frame=CGRectMake(0,0,320,400); 
    button1.frame=CGRectMake(0,400,320,400); 
    [textField resignFirstResponder]; 
    return YES; 

} 

不要忘記爲文本框添加代表