国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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

2019-11-14 18:01:51
字體:
來源:轉載
供稿:網友

 

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

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 麟游县| 青田县| 安化县| 安仁县| 广西| 凤山市| 建平县| 广汉市| 高雄县| 竹北市| 鹿泉市| 井陉县| 鹤山市| 富裕县| 工布江达县| 定州市| 密山市| 云安县| 本溪| 新巴尔虎右旗| 红桥区| 芮城县| 勃利县| 鱼台县| 普格县| 楚雄市| 全州县| 隆昌县| 云浮市| 长海县| 石景山区| 洛阳市| 秦皇岛市| 安西县| 甘南县| 抚顺县| 嵊泗县| 南丰县| 孙吴县| 清水县| 泾阳县|