UIScrollView中UITextField文本编辑框位置随键盘的显示(隐藏)改变

来源:互联网 发布:淘宝差评回复技巧 编辑:程序博客网 时间:2024/06/06 09:19

//

//  ViewBaseController.h

//  

//

//  Created bynokiaxjw on 12-12-6.

//  Copyright (c) 2012 All rights reserved.

//


#import <UIKit/UIKit.h>


@interface ViewBaseController :UIViewController<UITextFieldDelegate>


@property(nonatomic,strong)UITextField *activeTextField;

@property(nonatomic,assign)NSUInteger keyboardHeight;


//UITextFieldDelegate

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

- (void)textFieldDidEndEditing:(UITextField *)textField;

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

- (IBAction)onEditDone:(id)sender;

@end


//

//  ViewBaseController.m

//  Sb06D

//

//  Created bynokiaxjw on 12-12-6.

//  Copyright (c) 2012. All rights reserved.

//


#import "ViewBaseController.h"

#import "AppDelegate.h"

#import "MainViewController.h"


@interfaceViewBaseController ()


@end


@implementation ViewBaseController


- (id)initWithCoder:(NSCoder *)aDecoder

{

   self = [superinitWithCoder:aDecoder];

   if (self) {

       self.keyboardHeight = 352.0f;

        self.activeTextField =nil;

    }

    returnself;

}


- (void)dealloc

{

    [[NSNotificationCenterdefaultCenter]removeObserver:self];

 

}


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    AppDelegate *delegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];

    self.device = delegate.device;

    self.pile =self.device.pile;

    

    [selfregisterForKeyboardNotifications];

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (void) registerForKeyboardNotifications

{    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];

 [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];

}



#pragma mark -

#pragma mark Responding to keyboard events

- (void)keyboardWillShow:(NSNotification *)notification 

{

    

   /*

     Reduce the size of the text view so that it's not obscured by the keyboard.

     Animate the resize so that it's in sync with the appearance of the keyboard.

     */

    

   NSDictionary *userInfo = [notificationuserInfo];

    

    // Get the origin of the keyboard when it's displayed.

    NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];

    

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.

   CGRect keyboardRect = [aValueCGRectValue];

    

    // Get the duration of the animation.

    NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

   NSTimeInterval animationDuration;

    [animationDurationValuegetValue:&animationDuration];

    

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.

    if ( [self.viewisKindOfClass:[UIScrollViewclass]] )

    {

        UIInterfaceOrientation interfaceOrientation = [UIApplicationsharedApplication].statusBarOrientation;

        if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {

           self.keyboardHeight = keyboardRect.size.height;

        }

       else

           self.keyboardHeight = keyboardRect.size.width;


       UIScrollView *scrollView = (UIScrollView*)self.view;

        CGSize contentSize =CGSizeMake(scrollView.bounds.size.width,scrollView.bounds.size.height+self.keyboardHeight);

        scrollView.contentSize =contentSize;

        

        // If active text field is hidden by keyboard, scroll it so it's visible        

        // Your application might not need or want this behavior.

       CGRect aRect =self.view.bounds;

        aRect.size.height -=self.keyboardHeight;

        CGPoint pt =self.activeTextField.frame.origin;

        if ( pt.y+self.activeTextField.frame.size.height>(aRect.origin.y+aRect.size.height) )

        {   

            CGPoint scrollPoint =CGPointMake(0.0,  pt.y+self.activeTextField.frame.size.height-(aRect.origin.y+aRect.size.height));

            [scrollViewsetContentOffset:scrollPointanimated:YES];

        }

    }

    

}


- (void)keyboardWillHide:(NSNotification *)notification

{

    

   NSDictionary* userInfo = [notificationuserInfo];

   /*

     Restore the size of the text view (fill self's view).

     Animate the resize so that it's in sync with the disappearance of the keyboard.

     */

    NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

   NSTimeInterval animationDuration;

    [animationDurationValuegetValue:&animationDuration];

    

    if ( [self.viewisKindOfClass:[UIScrollViewclass]] )

    {

       UIScrollView *scrollView = (UIScrollView*)self.view;

       CGSize contentSize = scrollView.bounds.size;

        scrollView.contentSize =contentSize;

    }

 

}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    returnYES;

}


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

   self.activeTextField = textField;

}


- (void)textFieldDidEndEditing:(UITextField *)textField

{    

    self.activeTextField =nil;

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    self.activeTextField =nil;

    returnNO;

}


- (IBAction)onEditDone:(id)sender

{

    if (self.activeTextField!=nil)

        [self.activeTextFieldresignFirstResponder];

}

@end



编辑NIB文件,设置*.xib文件的File's Owner为ViewBaseController,*.xib的视图为Scroll View。修改Scroll View中的UITextField的delegate(Outlets)为File's Owner。
       
原创粉丝点击