2011-11-14 30 views
0

我有一個登錄屏幕,當用戶觸摸文本文件以輸入數據時,我想要將文本框向上移動,並在用戶觸摸屏幕上的任何位置時將文本框與鍵盤一起向下移動。如何上下移動UItexfield?

我能夠在用戶交互中移動文本字段,並且當用戶觸摸屏幕上的任何位置時我可以向下移動鍵盤,但是我無法將文本字段恢復到原始位置。

下面是我的代碼: .h文件中:

#import <UIKit/UIKit.h> 
#import "RegistrationForm.h" 
#import "sqlite3.h"; 



@interface VLoginViewController : UIViewController <UITableViewDelegate ,UITableViewDataSource> 

{ 

     NSDictionary *tableContents; 

    NSArray *textField; 

    UILabel *lblCompanyName; 


    UITextField *txtUserName; 

    UITextField *txtPass; 


    UIButton *btnLogin; 

    UIButton *btnSignUp; 

    RegistrationForm *registration; 

    NSString *dbPath; 

    sqlite3 *loginDB; 



} 

@property(nonatomic ,retain) NSDictionary *tableContents; 

@property(nonatomic ,retain) NSArray *textField; 

@property(nonatomic, retain) IBOutlet UITextField *txtUserName; 

@property(nonatomic ,retain) IBOutlet UITextField *txtPass; 

@property(nonatomic ,retain) UILabel *lblCompanyName; 

@property(nonatomic ,retain) UIButton *btnLogin; 

@property(nonatomic ,retain) UIButton *btnSignUp; 




@end 


this is my .m file: 




// LoginPage.m 
// MYBusinessCentral 
// 
// Created by Bcod Web on 09/11/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "LoginPage.h" 
#define kOFFSET_FOR_KEYBOARD 60.0 


@implementation LoginPage 
@synthesize imgView; 
@synthesize tableContents,textField; 
@synthesize txtUserName,txtPassword; 
@synthesize btn_login,btn_registration; 




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    NSArray *text = [[NSArray alloc]initWithObjects:@"" ,@"" ,nil]; 
    NSDictionary *textData = [[NSDictionary alloc]initWithObjectsAndKeys:text,@"",nil]; 
    self.tableContents = textData; 
    self.textField = [[self.tableContents allKeys] sortedArrayUsingSelector:@selector(compare:)]; 



    btn_login = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    btn_login.frame = CGRectMake(25, 260, 270, 40); 
    [btn_login setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal]; 
    [btn_login setTitle:@"login" forState:UIControlStateNormal]; 
    [btn_login addTarget:self action:@selector(Login:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:btn_login]; 



    [super viewDidLoad]; 
} 


-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:txtUserName] && [sender isEqual:txtPassword]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      //[self setViewMovedUp:YES]; 
     } 
    } 
} 


-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)keyboardWillShow:(NSNotification *)notif 
{ 
    //keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately 

    if ([txtUserName isFirstResponder] && self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 

    else if ([txtPassword isFirstResponder] && self.view.frame.origin.y >=0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
} 

-(void)textFieldShouldReturn:(UITextField *)sender 
{ 
    if ([sender isEqual:txtUserName] && [sender isEqual:txtPassword]) 
    { 
     if (self.view.frame.origin.y <=0) 
     { 
      //[self setViewMovedDown:YES]; 
     } 
    } 
} 

-(void)setViewMovedDown:(BOOL)movedDown 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedDown) 
    { 

     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 

- (void)keyboardWillHide:(NSNotification *)notif 
{ 
    //keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately 

    if ([txtUserName isFirstResponder] && self.view.frame.origin.y <= 0) 
    { 
     [self setViewMovedDown:YES]; 
    } 

    else if ([txtPassword isFirstResponder] && self.view.frame.origin.y <=0) 
    { 
     [self setViewMovedDown:YES]; 
    } 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification object:self.view.window]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification object:self.view.window]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 

} 

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [txtUserName resignFirstResponder]; 
    [txtPassword resignFirstResponder]; 

    NSLog(@"Touches began."); 

} 



/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [super dealloc]; 
} 


-(NSInteger) numberOfSectionInTableView:(UITableView *)tableView 
{ 
    return[self.textField count]; 
} 




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    NSArray *lsData =[self.tableContents objectForKey:[self.textField objectAtIndex:section]]; 
    return [lsData count]; 

} 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *TextCellIdentifier [email protected]"Cell"; 

    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier]; 

    if (cell==nil) 
    { 
     cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier ] autorelease]; 
     cell.accessoryType= UITableViewCellAccessoryNone; 
     cell.selectionStyle=UITableViewCellSelectionStyleNone; 
    } 






    if ([indexPath row] ==0) 
    { 
     txtUserName = [[UITextField alloc]initWithFrame:CGRectMake(12, 0, 250,20)]; 

     [email protected]"UserName"; 
     txtUserName.autocorrectionType =UITextAutocorrectionTypeNo; 
     [txtUserName setClearButtonMode:UITextFieldViewModeWhileEditing]; 
     txtUserName.keyboardType =UIKeyboardTypeEmailAddress; 
     txtUserName.textAlignment =UITextAlignmentLeft; 
     txtUserName.font=[UIFont fontWithName:@"Helvetica" size:15]; 
     txtUserName.adjustsFontSizeToFitWidth =YES; 
     [txtUserName setEnabled:YES]; 
     cell.accessoryView =txtUserName; 
     //[cell addSubview:txtUserName]; 
     [txtUserName release]; 

    } 



    if([indexPath row]==1)  
    { 
     txtPassword = [[UITextField alloc]initWithFrame:CGRectMake(12, 0, 250,20)]; 
     [email protected]"Password"; 
     txtPassword.secureTextEntry =YES; 
     txtPassword.autocorrectionType =UITextAutocorrectionTypeNo; 
     [txtPassword setClearButtonMode:UITextFieldViewModeWhileEditing]; 
     txtPassword.keyboardType =UIKeyboardTypeEmailAddress; 
     txtPassword.textAlignment =UITextAlignmentLeft; 
     txtUserName.font=[UIFont fontWithName:@"Helvetica" size:15]; 
     txtPassword.adjustsFontSizeToFitWidth =YES; 
     [txtPassword setEnabled:YES]; 
     cell.accessoryView=txtPassword;  
     //[cell addSubview:txtPass]; 
     [txtPassword release]; 

    } 



    return cell; 

} 


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 


} 



-(IBAction)Login:(id)sender 
{ 
} 

-(IBAction)Registration:(id)sender 
{ 
} 



@end 

回答

1

試試這個方法:

設置文本框1日委託。

-(void)textFieldDidBeginEditing:(UITextField *)textField {   

[UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.5]; 
    [textField setFrame:CGRectMake(50,50,100,40)]; 
    [UIView commitAnimations]; 

} 

當鍵盤返回鍵按下時,將文本字段設置爲原始位置。

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

[UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:1.5]; 
    [textField setFrame:CGRectMake(50,200,100,40)]; 
    [UIView commitAnimations]; 
} 

在觸摸的

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
[textField resignFirstResponder]; 

[UIView beginAnimations:nil context:nil]; 
     [UIView setAnimationDuration:1.5]; 
     [textField setFrame:CGRectMake(50,200,100,40)]; 
     [UIView commitAnimations]; 

NSLog(@"Touches began."); 

} 
+0

謝的先生,但是它不工作........,我想下移當用戶觸摸任何視圖時的文本框 – Nilesh

+0

然後添加觸摸事件。我已經更新了代碼請看看它。 – Anand

+0

我沒有得到你..... anandji可以請你提供我的整個代碼或任何教程........這將是有益的....謝謝 – Nilesh

0

有拖委託您可以使用此方法。

1.-(void)textFieldDidBeginEditing:(UITextField *)textField   
2.-(BOOL)textFieldShouldReturn:(UITextField *)textField 

1.in didbegin編輯

textfield.frame = CGRectMake(0,-10,100,40); 

2.in shouldreturn方法

textfield.frame = CGRectMake(0,10,100,40);