Scrolling Content and Returnin when Keyboard Shown and Hidden?

Discussion in 'iOS Programming' started by newtoiphonesdk, Oct 5, 2011.

  1. newtoiphonesdk macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    This used to work in iOS 3.0, but I know some things have changed since then. Could someone point me in the correct direction to get this fixed? Here is my current code.
    Code:
    
    #import "Blank_FillerViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @implementation Blank_FillerViewController
    
    @synthesize scroll;
    @synthesize textField;
    @synthesize textFeild;
    @synthesize scrollView;
    #define kOFFSET_FOR_KEYBOARD 185.0
    
    - (void)keyboardWasShown:(NSNotification*)aNotification {
        if (keyboardShown) return;
    	
        NSDictionary* info = [aNotification userInfo];
    	
        // Get the size of the keyboard.
        NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
    	
        // Resize the scroll view (which is the root view of the window)
        CGRect viewFrame = [scrollView frame];
        viewFrame.size.height -= keyboardSize.height;
        scrollView.frame = viewFrame;
    	
        // Scroll the active text field into view.
        CGRect textFieldRect = [activeField frame];
        [scrollView scrollRectToVisible:textFieldRect animated:YES];
    	
        keyboardShown = YES;
    }
    
    - (void)keyboardWasHidden:(NSNotification*)aNotification {
        NSDictionary* info = [aNotification userInfo];
    	
        // Get the size of the keyboard.
        NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
    	
        // Reset the height of the scroll view to its original value
        CGRect viewFrame = [scrollView frame];
        viewFrame.size.height += keyboardSize.height;
        [scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:YES];
        scrollView.frame = viewFrame;
    	
        keyboardShown = NO;
    }
    
    - (void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
    	for (UIView* view in self.view.subviews) {
    		if ([view isKindOfClass:[UITextField class]])
    			[view resignFirstResponder];
    	}
    }
    - (BOOL)textFieldShouldReturn: (UITextField *)textField {
    	[textField resignFirstResponder];
    	return YES;
    }
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
    	
    	sleep(3);
        [super viewDidLoad];
    	
        [[NSNotificationCenter defaultCenter] addObserver: self 
    											 selector: @selector(keyboardWasShown:)
    												 name: UIKeyboardDidShowNotification 
    											   object: nil];
    	
        [[NSNotificationCenter defaultCenter] addObserver: self
    											 selector: @selector(keyboardWasHidden:)
    												 name: UIKeyboardDidHideNotification 
    											   object: nil];
    	
    	keyboardShown = NO; // 1
    	[scrollView setContentSize: CGSizeMake( 320, 480)]; // 2
    	scrollView.maximumZoomScale = 4.0;
    	scrollView.minimumZoomScale = 0.75;
    	scrollView.clipsToBounds = YES;
    	scrollView.delegate = self;
        [scrollView addSubview:imageview];
    		
    	
    }
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    	return imageview;
    }
    
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        activeField = textField;
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        activeField = nil;
    }
    
    
    
    
    
    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        // Return YES for supported orientations
        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 {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    @end
    
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    There were changes in OS 3.2 when iPad was released and 4.0 for iPhone. The recommended keys to inspect in the keyboard notifications changed. Apple docs have sample code and you can easily google for example code.
     

Share This Page