2013年7月19日 星期五

iOS 擊點背景讓textField鍵盤縮下去

step1.
新增一個UITextField出來

UITextField *myTextField=[[UITextField alloc]initWithFrame:CGRectMake(
121, 30, 159, 30) ];


 self.nameTextField.placeholder = @"請輸入xxxx";

self.myTextField.delegate = self ;//要設定代理人


step2.
設定按背景

if(&UIViewNoIntrinsicMetric)
{
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [self.view addGestureRecognizer:tapGesture];
        
}

step3. 按下後執行的副程式

-(void)tap:(id)sender
{
    [self.view endEditing:YES];
    //[ScrollView setContentOffset:CGPointMake(0, 0) animated:YES]; //會幫你移動道你要顯示的行數
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

=================================================

顯示keyboard:
[myTextField becomeFirstResponder];

隱藏keyboard:
[myTextField resignFirstResponder];

endEditing:的方法
下面介紹一下endEditing:的方法,該方法為UIView中的一個方法,定義為

- (BOOL)endEditing:(BOOL)force

官方原文

Causes the view (or one of its embedded text fields) to resign the first responder status.
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign.
翻譯為
註銷當前view(或它下屬嵌入的test fidlds)的first responder狀態
該方法在view中找到處於first responder狀態的test fiedld,如果找到會註銷其狀態,如果指定force參數為YES,則不再詢問test field,而直接強制註銷其他first responder狀態






0 意見:

張貼留言