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

//

//  ViewBaseController.h

//  

//

//  Created by nokiaxjw 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 by nokiaxjw on 12-12-6.

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

//


#import "ViewBaseController.h"

#import "AppDelegate.h"

#import "MainViewController.h"


@interface ViewBaseController ()


@end


@implementation ViewBaseController


- (id)initWithCoder:(NSCoder *)aDecoder

{

   self = [superinitWithCoder:aDecoder];

   if (self) {

       self.keyboardHeight = 352.0f;

        self.activeTextField =nil;

    }

    return self;

}


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

{

    return YES;

}


- (void)textFieldDidBeginEditing:(UITextField *)textField

{

   self.activeTextField = textField;

}


- (void)textFieldDidEndEditing:(UITextField *)textField

{    

    self.activeTextField =nil;

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    [textField resignFirstResponder];

    self.activeTextField =nil;

    return NO;

}


- (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。
       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值