2013-02-13 26 views
2

我試圖以編程方式創建一些文本字段。它們出現在屏幕的底部,當選擇鍵盤時它們覆蓋它們。當選擇文本框時,我需要將視圖向上移動。當UITextField被選中時移動UIView

這裏是我的視圖代碼:

頁眉:

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> { 
    IBOutlet UITextField *usernameField; 
} 

小鬼查看文件:

// Create sub view for Logo 
    UIView *logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,280)]; 
    [logoView setBackgroundColor:[UIColor blueColor]]; 
    logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

    // Create sub view for fields 
    UIView *fieldView = [[UIView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, 200)]; 
    [fieldView setBackgroundColor:[UIColor greenColor]]; 
    fieldView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin; 

    // Setting up the text fields 
    // Username field 
    usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 300, 40)]; 
    usernameField.borderStyle = UITextBorderStyleRoundedRect; 
    usernameField.textColor = [UIColor blackColor]; 
    usernameField.font = [UIFont systemFontOfSize:17.0]; 
    usernameField.placeholder = @"Username"; 
    usernameField.backgroundColor = [UIColor whiteColor]; 
    usernameField.autocorrectionType = UITextAutocorrectionTypeNo; 
    usernameField.keyboardType = UIKeyboardTypeDefault; 
    usernameField.returnKeyType = UIReturnKeySearch; 
    usernameField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    usernameField.delegate = self; 

    // Password field 
    UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(10, (usernameField.bounds.size.height + 30), 300, 40)]; 
    passwordField.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin; 
    passwordField.borderStyle = UITextBorderStyleRoundedRect; 
    passwordField.textColor = [UIColor blackColor]; 
    passwordField.font = [UIFont systemFontOfSize:17.0]; 
    passwordField.placeholder = @"Password"; 
    passwordField.backgroundColor = [UIColor whiteColor]; 
    passwordField.autocorrectionType = UITextAutocorrectionTypeNo; 
    passwordField.keyboardType = UIKeyboardTypeDefault; 
    passwordField.returnKeyType = UIReturnKeySearch; 
    passwordField.clearButtonMode = UITextFieldViewModeWhileEditing; 
    passwordField.delegate = self; 

    // Add fields/button to fieldView 
    [fieldView addSubview:usernameField]; 
    [fieldView addSubview:passwordField]; 

    // Add subviews to main view 
    [self.view addSubview:logoView]; 
    [self.view addSubview:fieldView]; 

我加入這個方法用於移動視圖,但不能正常工作了最後一部分:

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
    NSLog(@"textFieldDidBeginEditing"); 
    //If we begin editing on the text field we need to move it up to make sure we can still 
    //see it when the keyboard is visible. 
    //I am adding an animation to make this look better 
    [UIView beginAnimations:@"Animate Text Field Up" context:nil]; 
    [UIView setAnimationDuration:.3]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    usernameField.frame = CGRectMake(usernameField.frame.origin.x, 
             -200, 
             usernameField.frame.size.width, 
             usernameField.frame.size.height); 

    [UIView commitAnimations]; 

} 

我實際上正在尋找的是完全轉移視圖,或者可能將其更改爲上面的可滾動區域。儘管我在觸摸endEditing之後實現了另一種方法。

看來,如果我移動一個領域,我將不得不完成代碼移動所有...我怎樣才能移動視圖(完整視圖)?

視圖在方法textFieldDidEndEditing | textFieldShouldEndEditing中返回到正常狀態還需要什麼?

回答

0

當您在鍵盤變爲選定狀態後設置動畫時 - 改爲爲整個視圖設置動畫。

首先聲明UIView的變量,使之獲得:

@interface MESLoginViewController : UIViewController <PFLogInViewControllerDelegate, UITextFieldDelegate> 
{ 
IBOutlet UITextField *usernameField; 
UIView *fieldView; 
} 

然後在您的實現創造這一觀點

// Create sub view for Logo 
UIView *logoView =[[UIView alloc] initWithFrame:CGRectMake(0,0,320,280)]; 
[logoView setBackgroundColor:[UIColor blueColor]]; 
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

// Create sub view for fields 
fieldView = [[UIView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, 200)]; 
[fieldView setBackgroundColor:[UIColor greenColor]]; 
fieldView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin; 

// Setting up the text fields 
// Username field 
usernameField = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, 300, 40)]; 
usernameField.borderStyle = UITextBorderStyleRoundedRect; 
usernameField.textColor = [UIColor blackColor]; 
usernameField.font = [UIFont systemFontOfSize:17.0]; 
usernameField.placeholder = @"Username"; 
usernameField.backgroundColor = [UIColor whiteColor]; 
usernameField.autocorrectionType = UITextAutocorrectionTypeNo; 
usernameField.keyboardType = UIKeyboardTypeDefault; 
usernameField.returnKeyType = UIReturnKeySearch; 
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing; 
usernameField.delegate = self; 

// Password field 
UITextField *passwordField = [[UITextField alloc] initWithFrame:CGRectMake(10, (usernameField.bounds.size.height + 30), 300, 40)]; 
passwordField.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin; 
passwordField.borderStyle = UITextBorderStyleRoundedRect; 
passwordField.textColor = [UIColor blackColor]; 
passwordField.font = [UIFont systemFontOfSize:17.0]; 
passwordField.placeholder = @"Password"; 
passwordField.backgroundColor = [UIColor whiteColor]; 
passwordField.autocorrectionType = UITextAutocorrectionTypeNo; 
passwordField.keyboardType = UIKeyboardTypeDefault; 
passwordField.returnKeyType = UIReturnKeySearch; 
passwordField.clearButtonMode = UITextFieldViewModeWhileEditing; 
passwordField.delegate = self; 

// Add fields/button to fieldView 
[fieldView addSubview:usernameField]; 
[fieldView addSubview:passwordField]; 

// Add subviews to main view 
[self.view addSubview:logoView]; 
[self.view addSubview:fieldView]; 

然後動畫的FieldView代替:

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
NSLog(@"textFieldDidBeginEditing"); 
//If we begin editing on the text field we need to move it up to make sure we can still 
//see it when the keyboard is visible. 
//I am adding an animation to make this look better 
[UIView beginAnimations:@"Animate Text Field Up" context:nil]; 
[UIView setAnimationDuration:.3]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 

fieldView.frame = CGRectMake(fieldView.frame.origin.x, 
            -200, 
            fieldView.frame.size.width, 
            fieldView.frame.size.height); 

[UIView commitAnimations]; 

}

由於所有文本字段都是fieldView的子視圖,它們將與它一起動畫。這應該排序它!

+0

謝謝,但然後標識視圖不會轉移,所以如果視圖不包括標識視圖,你會看到重疊。無論如何移動整個視圖。我應該創建另一個變量是整個視圖,並更新origin.y而不是? – StuartM 2013-02-19 18:32:30

+0

哦,我的錯誤 - 輕鬆修復。在textFieldDidBeginEditing方法中使用self.view.frame而不是fieldView.frame,這將移動整個視圖。 – TiernanKennedy 2013-02-19 21:56:33

+0

這是否解決了您的問題? – TiernanKennedy 2013-02-21 19:51:45

0

您正在訪問我朋友的方法中的本地變量usernameField。這是編譯器錯誤。

+0

好吧,我已經更新了代碼,我會更新的問題。我現在可以使場也移動,但我實際上喜歡整個視圖,而不是僅更改文本字段的單個幀... – StuartM 2013-02-13 17:02:24

+0

謝謝,問題已更新。 – StuartM 2013-02-13 21:38:35

+0

你可以將你想要的所有視圖逐個或整個視圖本身移動。你有代碼爲usernameField進行轉換,爲什麼不把它用於其他視圖? – mkirci 2013-02-14 08:02:16

0

如果我的理解是正確的 - 你實際上想要改變「fieldView」 - 因爲兩個UITextFields是這個視圖的子視圖。

您拋出該錯誤的原因是因爲該變量在該方法中無法訪問 - 您需要在頭文件中聲明一個實例變量。

+0

正確。我用更正的代碼更新了這個問題,但仍然不確定如何移動整個視圖。 – StuartM 2013-02-13 21:39:05

+0

您可以移動fieldView - 其中包含您的用戶名和密碼字段 - 或整個視圖(self.view) – TiernanKennedy 2013-02-14 02:50:12

+0

我不確定如何...如何移動整個視圖,包括子視圖是實際問題。 – StuartM 2013-02-14 08:13:40

0

只需在你的.h和.m文件中使用下面的代碼,它對我來說工作的很好。

-------的.h -----

CGFloat animatedDistance; 

--------。米----------

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 

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

    CGRect textFieldRect = 
    [self.view.window convertRect:textField.bounds fromView:textField]; 
    CGRect viewRect = 
    [self.view.window convertRect:self.view.bounds fromView:self.view]; 
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
    CGFloat numerator = 
    midline - viewRect.origin.y 
    - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = 
    (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) 
    * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 

    if (heightFraction < 0.0) 
    { 
     heightFraction = 0.0; 
    } 
    else if (heightFraction > 1.0) 
    { 
     heightFraction = 1.0; 
    } 

    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 

    if (orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    } 
    else 
    { 
     //animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
    } 

    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y -= animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y += animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 
} 

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

- (IBAction) textFieldReturn:(id)textField 
{ 
    [textField resignFirstResponder]; 
} 

我已經得到了solution here

相關問題