2011-10-22 82 views
0
#import "LoginScreen.h" 

#define kTabBarHeight 1 
#define kKeyboardAnimationDuration 0.3 


@implementation LoginScreen 

@synthesize userName,password,loginButton,scrollView; 

BOOL keyboardIsShown; 


// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization. 

    } 
    return self; 
} 


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

    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification 
               object:self.view.window]; 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification 
               object:self.view.window]; 
    keyboardIsShown = NO; 
    //make contentSize bigger than your scrollSize (you will need to figure out for your own use case) 
    // CGSize scrollContentSize = CGSizeMake(1024,700); 
    // [scrollView setContentSize : scrollContentSize]; 
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
} 



- (void)keyboardWillHide:(NSNotification *)n 
{ 
    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; 
    CGSize keyboardSize = [boundsValue CGRectValue].size; 


    // resize the scrollview 
    CGRect viewFrame = self.scrollView.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    viewFrame.size.height += (keyboardSize.height - kTabBarHeight); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    // The kKeyboardAnimationDuration I am using is 0.3 
    [UIView setAnimationDuration:kKeyboardAnimationDuration]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = NO; 
} 

- (void)keyboardWillShow:(NSNotification *)n 
{ 
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown. 
    if (keyboardIsShown) { 
     return; 
    } 

    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; 
    CGSize keyboardSize = [boundsValue CGRectValue].size; 

    // resize the noteView 
    CGRect viewFrame = self.scrollView.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    viewFrame.size.height -= (keyboardSize.height - kTabBarHeight); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    // The kKeyboardAnimationDuration I am using is 0.3 
    [UIView setAnimationDuration:kKeyboardAnimationDuration]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = YES; 
} 



- (IBAction) loginButton: (id) sender{ 


} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Overriden to allow any orientation. 
    return YES; 
} 


- (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; 

    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillShowNotification 
                object:nil]; 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillHideNotification 
                object:nil]; 

} 



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


@end 

有人可以告訴我在下面的代碼中出現了什麼問題。即使我將鍵盤高度減去框架高度,原始視圖也不會移動。在Xcode中彈出鍵盤時向上移動框架

當鍵盤彈出時,scrollView不會向上移動?我在這裏錯過了一些代碼嗎?

回答

1

嗯,問題是你沒有告訴你的代碼在哪裏你的文本字段開始,所以它不知道它需要從哪裏滾動。你可能認爲你的self.scrollView.frame做到了,但它只是告訴代碼你的ScrollView的大小,而不是它需要滾動或滾動。這是我做我的工作。

查看您的某個文本字段的連接。從「開始編輯」和「結束編輯」拖放到.h文件中以創建IBAction函數聲明。 Xcode是打算把功能集成到了.M,但換成:

//even though these functions don't reference the IBAction that we placed for the 
"DidBeginEditing" sender for a text field, it will still call these functions. 
We need to let the code know what text field we just touched so we can go through 
the functions that reset the view size if the text field is under the keyboard. 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    currentTextField = textField; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    currentTextField = nil; 
} 

這提醒了我,你就需要在.M申報currentTextField作爲一個的UITextField *:

@interface ThirdView : UIViewController{ 
    UITextField *currentTextField; 
    BOOL keyboardIsShown; 
} 

我viewDidLoad中,keyBoardWasShown等都是從你的有點不同,但我只是把他們都在這裏,所以你可以看到我是如何得到它的工作:

//This is code you actually add to get the view to scroll. 
You should first connect an outlet from the ScrollView to the .h file so these 
functions become available. 

- (void)viewDidLoad { 


    //standard screen size is 320 X 460 

    // ---set the viewable frame of the scroll view--- 

    // scrollView.frame = CGRectMake(0, 0, 320, 460); 


    //---set the content size of the scroll view--- 

    // [scrollView setContentSize:CGSizeMake(320, 615)]; 


    //the status bar is 20 pixels tall 

    //the navigation bar is 44 pixels tall 


    //---set the viewable frame of the scroll view--- 

    //Note: for some reason, the origin (0,44) doesn't take into account the status bar, but it works anyway. However, the height of the scroll view does take it into account. Wierd, but whatever. So you have to make y1 = 44, and y2 = 460-44-20. 

     scrollView.frame = CGRectMake(0, 44, 320, 416); 

    //---set the content size of the scroll view--- 

     [scrollView setContentSize:CGSizeMake(320, 571)]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

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

    [super viewDidLoad]; 
} 



- (void)viewDidUnload 
{ 

    [self setPrincipal_Amt:nil]; 

    [self setAPR:nil]; 

    [self setYears:nil]; 

    [self setScrollView:nil]; 

    [super viewDidUnload]; 

    // Release any retained subviews of the main view. 

    // e.g. self.myOutlet = nil; 

} 

// Called when the UIKeyboardDidShowNotification is sent. 

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 

    if(keyboardIsShown) 
     return; 

    NSDictionary* info = [aNotification userInfo]; 

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 

    scrollView.contentInset = contentInsets; 

    scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    Notice that we've got to add 64 pixels to the keyboard height because the CGRect has 
    no idea that you have the status bar shown and a navigation bar within your view, so 
    you have to manually add it in. 

    CGRect aRect = self.view.frame; 

    aRect.size.height -= (kbSize.height + 64); 

    if (!CGRectContainsPoint(aRect, currentTextField.frame.origin)) 
    { 
     CGPoint scrollPoint = CGPointMake(0.0, currentTextField.frame.origin.y-kbSize.height + 64); 

     [scrollView setContentOffset:scrollPoint animated:YES]; 

    } 

    keyboardIsShown = YES; 

} 

// Called when the UIKeyboardWillHideNotification is sent 

- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 

    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 

    scrollView.contentInset = contentInsets; 

    scrollView.scrollIndicatorInsets = contentInsets; 


    keyboardIsShown = NO; 

} 
// 

我希望幫助...即使你確實發佈了這個前段時間:)我花了一段時間纔得到它的工作。非常令人沮喪。

相關問題