關於輸入框被鍵盤覆蓋及收回鍵盤的問題,輸入框收回

來源:互聯網
上載者:User

關於輸入框被鍵盤覆蓋及收回鍵盤的問題,輸入框收回

 

-----------ViewController.m中的內容------------

#import "ViewController.h"

#import "ScreenView.h"

@interface ViewController ()<UITextFieldDelegate>

{

    ScreenView *_screenV;//覆蓋全屏

    UIView *secView;

}

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    [self textFieldOnView:self.view];//添加子視圖

    [self coverBtnOnView:self.view];//添加按鈕

}

- (void)textFieldOnView:(UIView *)currentV{

    UITextField *tF1 = [[UITextField alloc]init];

    tF1.frame = CGRectMake(30, 100, 100, 40);

    tF1.delegate = self;

#pragma ------設定tF1.tag = 1;

    tF1.tag = 1;

    tF1.borderStyle = UITextBorderStyleRoundedRect;

    [currentV addSubview: tF1];

    

    UITextField *tF2 = [[UITextField alloc]init];

    tF2.frame = CGRectMake(30, 500, 100, 40);

    tF2.delegate = self;

    tF2.borderStyle = UITextBorderStyleRoundedRect;

    [currentV addSubview: tF2];

    

    secView = [[UIView alloc]initWithFrame:CGRectMake(150, 200, 200, 300)];

    secView.backgroundColor = [UIColor yellowColor];

    [currentV addSubview:secView];

    

    UITextField *textF1 = [[UITextField alloc]init];

    textF1.frame = CGRectMake(30, 100, 100, 40);

    textF1.delegate = self;

    textF1.borderStyle = UITextBorderStyleRoundedRect;

    [secView addSubview: textF1];

    

    UITextField *textF2 = [[UITextField alloc]init];

    textF2.frame = CGRectMake(30, 250, 100, 40);

    textF2.delegate = self;

    textF2.borderStyle = UITextBorderStyleRoundedRect;

    [secView addSubview: textF2];

}

 

- (void)coverBtnOnView:(UIView *)currentV{

    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    coverBtn.backgroundColor = [UIColor cyanColor];

    if (!_screenV) {

        [coverBtn setTitle:@"覆蓋全屏" forState:UIControlStateNormal];

        [coverBtn addTarget:self action:@selector(coverScreen) forControlEvents:UIControlEventTouchUpInside];

    }else{

        [coverBtn setTitle:@"返回" forState:UIControlStateNormal];

        [coverBtn addTarget:self action:@selector(removeCoverScreen) forControlEvents:UIControlEventTouchUpInside];

    }

    

    UIView *v = [self.view viewWithTag:1];

    coverBtn.frame = v.frame;

    

    CGRect temp = coverBtn.frame;

    temp.origin.x += temp.size.width;

    coverBtn.frame = temp;

    

    [currentV addSubview:coverBtn];

}

 

- (void)removeCoverScreen{

    [_screenV removeFromSuperview];

}

 

- (void)coverScreen{

    _screenV = [[ScreenView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    

#pragma -----視圖本身透明度

    _screenV.backgroundColor = [[UIColor brownColor]colorWithAlphaComponent:0.5];

#pragma -----視圖及其子視圖透明度

    //    screenV.alpha = 0.7;

    

#pragma -----建立view覆蓋全屏,需要建立如ScreenView類,重寫touchesBegan方法

    [[UIApplication sharedApplication].keyWindow addSubview:_screenV];

#pragma -----如果self.view是全屏大小,下句可行,_screenV直接用UIView初始化,並能響應觸摸事件

//    [self.view addSubview:_screenV];

    

    [self textFieldOnView:_screenV];//添加textField

    [self coverBtnOnView:_screenV];

}

 

//開始編輯輸入框的時候,軟鍵盤出現,執行此事件

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

    UIView *tempView = self.view;

    if (_screenV) {

        tempView = _screenV;

    }

    

    UIWindow *window = [[[UIApplication sharedApplication] delegate]window];

    CGRect rect = [textField convertRect:textField.bounds toView:window];//將textField相對於其父視圖的座標轉換成相對於window的座標

    

    NSInteger y = rect.origin.y + textField.frame.size.height;

    NSInteger offset = (y - (tempView.frame.size.height - 256.0));//鍵盤高度216 //如何擷取鍵盤高度?

    

    

    NSTimeInterval animationDuration = 0.30f;

    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];

    [UIView setAnimationDuration:animationDuration];

    

    //將視圖的Y座標向上移動offset個單位,以使下面騰出地方用於軟鍵盤的顯示

    if(offset > 0)

        tempView.frame = CGRectMake(0.0f, -offset, tempView.frame.size.width, tempView.frame.size.height);

    

    [UIView commitAnimations];

}

 

////當使用者按下return鍵或者按斷行符號鍵,keyboard消失

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

//{

//    [textField resignFirstResponder];

//    return YES;

//}

 

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

    [super touchesBegan:touches withEvent:event];

    [self.view endEditing:YES];

}

 

//輸入框編輯完成以後,將視圖恢複到原始狀態

-(void)textFieldDidEndEditing:(UITextField *)textField

{

    UIView *tempView = self.view;

    if (_screenV) {

        tempView = _screenV;

    }

    tempView.frame =CGRectMake(0, 0, tempView.frame.size.width, tempView.frame.size.height);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

-----------------ScreenView.m中的內容-----------------

#import "ScreenView.h"

 

@implementation ScreenView

 

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

    [super touchesBegan:touches withEvent:event];

    [self endEditing:YES];

}

@end

 

有疏漏之處敬請指正。

END

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.