zoukankan      html  css  js  c++  java
  • ios TextField 不被键盘遮住

         

         

        首先放一个scrollView窗口,将Scroll View视图占整个屏幕。

         向Scroll View    添加TextField 控件。


           首先,ViewController.h  代码如下;

        #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController<UITextFieldDelegate>
    {
        BOOL keyboardVisible;
    }
    @property (weak,nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak,nonatomic) IBOutlet UITextField *textField;
    
    @end



    ViewController.m 代码如下:

    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        self.scrollView.contentSize = CGSizeMake(320,600);
    
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        self.textField.delegate = self;
        //[super viewDidLoad];
    
    }
    //页面加载前调用的方法,注册两个通知:一个是键盘弹出来的通知,另外一个是键盘隐藏的通知,不同的通知调用不同的方法进行处理
    -(void) viewWillAppear:(BOOL)animated{
        //键盘弹起的通知
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(keyboardDidShow:)
         name:UIKeyboardDidShowNotification
         object:self.view.window];
        //键盘隐藏的通知
        [[NSNotificationCenter defaultCenter]
         addObserver:self
         selector:@selector(keyboardDidHide:)
         name:UIKeyboardDidHideNotification
         object:nil];
    }
    -(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{
        [textFieldView resignFirstResponder];
        return NO;
    }
    //收到键盘弹出事件
     -(void)keyboardDidShow:(NSNotification *)notif
    {
        if(keyboardVisible)
        {
            return;
        }
        
        NSDictionary * info = [notif userInfo];
        NSValue * aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        
        CGRect viewFrame = self.scrollView.frame;
        viewFrame.size.height -= (keyboardSize.height);
        self.scrollView.frame = viewFrame;
        
        CGRect textFieldRect =[self.textField frame];
        [self.scrollView scrollRectToVisible:textFieldRect animated:YES];
        keyboardVisible = YES;
        
        
    }
    // 收到键盘隐藏事件
    
    -(void)keyboardDidHide:(NSNotification*)note{
        
        NSDictionary * info = [note userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        
        CGRect viewFrame = self.scrollView.frame;
        viewFrame.size.height += (keyboardSize.height);
        self.scrollView.frame = viewFrame;
        
        if(!keyboardVisible)
        {
            return;
        }
        keyboardVisible = NO;
    
        
    }
    
    //页面消失前取消通知
    -(void)viewWillDisappear:(BOOL)animated{
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:UIKeyboardDidShowNotification
         object:nil];
        
        [[NSNotificationCenter defaultCenter]
         removeObserver:self
         name:UIKeyboardDidHideNotification
         object:nil];
    }
    


  • 相关阅读:
    ubuntu lock
    ubuntu 源
    ubuntu server版 ssh配置有时没有sshd_config文件或者空文件的情况
    pip3 install tensorflow==2.2
    tensorflow安装提示load 失败
    wXgame上某游戏封包分析
    lazarus 使用微软detour库 delphi
    dll函数导出
    Error: Duplicate resource: Type = 24, Name = 1, Lang ID = 0000
    Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 0.065 s <<< FAILURE!
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621102.html
Copyright © 2011-2022 走看看