鍵盤的出現於隱藏(解決鍵盤彈出時會覆蓋文字框的問題,代碼實現),鍵盤文字框

來源:互聯網
上載者:User

鍵盤的出現於隱藏(解決鍵盤彈出時會覆蓋文字框的問題,代碼實現),鍵盤文字框

鍵盤的出現於隱藏(解決鍵盤彈出時會覆蓋文字框的問題,代碼實現)

 

#import "ViewController.h"

#import "UIView+FrameExtension.h" // 可以自己寫,以後用著方便

#define kDeviceHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    // 設定視圖的背景色

    self.view.backgroundColor = [UIColor lightGrayColor];

    

    // 添加第一個文字框 假定位置

    UITextField *firstField = [[UITextField alloc]initWithFrame:CGRectMake(50, 300, 200, 40)];

    firstField.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:firstField];

    

    // 添加第一個文字框

    UITextField *secondField = [[UITextField alloc]initWithFrame:CGRectMake(firstField.x, firstField.bottom + 50, firstField.width , firstField.height)];

    [self.view addSubview:secondField];

    secondField.backgroundColor = [UIColor whiteColor];

    

    // 註冊鍵盤顯示的通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];

    // 註冊鍵盤隱藏的通知

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

}

 

// 鍵盤彈出時執行這個方法,

-(void)showKeyboard:(NSNotification *)notification{

    

    // 定義一個文字框,指向正在編輯的文字框,也就是彈出鍵盤的文字框

    UITextField *txtField;

    // 今次遍曆當前視圖的所有子視圖, subViews數組儲存的是當前視圖所有的子視圖

    for (UIView *subView in self.view.subviews) {

        // 如果這個子視圖是一個文字框的話,isKindOfClass方法可以判斷某個變數是不是某個類型的變數

        if ([subView isKindOfClass:[UITextField class]]) {

            // 先把這個子視圖轉化為文字框

            UITextField *tempField = (UITextField *)subView;

            // 再判斷這個文字框是不是正在編輯

            if (tempField.isEditing ) {

                // 如果這個文字框正在編輯,就是我要找的文字框,中斷迴圈

                txtField = tempField;

                break;

            }

        }

    }

    

    NSLog(@"%@", notification);

    // 擷取通知的userInfo屬性

    NSDictionary *userInfoDict = notification.userInfo;

    // 通過鍵盤通知的userInfo屬性擷取鍵盤的bounds

    NSValue *value = [userInfoDict objectForKey:UIKeyboardBoundsUserInfoKey];

    // 鍵盤的大小

    CGSize keyboardSize = [value CGRectValue].size;

    // 鍵盤高度

    CGFloat keyboardHeight = keyboardSize.height;

    

    CGFloat offset = kDeviceHeight - keyboardHeight - txtField.bottom ;

    

    if (offset < 0 ) {      //這種情況下需要上移

        offset = offset - 10 ;     //儲存上移的高度

        

        [UIView animateWithDuration:0.5 animations:^{

            

            self.view.transform = CGAffineTransformMakeTranslation(0, offset );

        }];

    }

    

}

 

-(void)hideKeyboard:(NSNotification *)notification{

    

    [UIView animateWithDuration:2 animations:^{

        

        self.view.transform = CGAffineTransformIdentity;

    }];

    

}

   // 點擊螢幕空白時隱藏鍵盤

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];

}

@end

 

 源檔案可以在這裡下載,希望可以幫到你:http://pan.baidu.com/s/1pLms9zP

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.