zoukankan      html  css  js  c++  java
  • iOS.UIKit.05.UIScrollView

    一、案例介绍:利用UIScrollView, 避免键盘遮挡控件;图01、图02其实是存在第三个UIButton的,滑动屏幕可以看到,如图06。效果如图01,图02。

    图01图02图03图06

    二、案例步骤:

    1、选择Single View Application新建项目,取名cq.30.ScrollView,如图03。

    2、Main.storyboard如图04,图05。

    图04图05

    3、CQ30ViewController.h

    #import <UIKit/UIKit.h>
    
    @interface CQ30ViewController : UIViewController<UITextFieldDelegate>
    {
        BOOL keyboardVisible;   //键盘出现标识
    }
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak, nonatomic) IBOutlet UITextField *textField;
    @end

    4、CQ30ViewController.m

    》通过UITextFieldDelegate实现对UITextField放弃第一响应者身份,关闭键盘

    #pragma mark -- UITextFieldDelegate method
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [textField resignFirstResponder];
        return YES;
    }

    》注册监听键盘打开与关闭通知

    -(void) viewWillAppear:(BOOL)animated {
        
        //注册键盘出现通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:)
                                                     name: UIKeyboardDidShowNotification object:nil];
        //注册键盘隐藏通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:)
                                                     name: UIKeyboardDidHideNotification object:nil];
        [super viewWillAppear:animated];
    }
    
    
    -(void) viewWillDisappear:(BOOL)animated {
        //解除键盘出现通知
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name: UIKeyboardDidShowNotification object:nil];
        //解除键盘隐藏通知
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name: UIKeyboardDidHideNotification object:nil];
        
        [super viewWillDisappear:animated];
    }

    》通过监听键盘的打开与关闭动态改变UIScrollView的frame,聚焦当前UITextField。

    -(void) keyboardDidShow: (NSNotification *)notif {
        
        if (keyboardVisible) {//键盘已经出现要忽略通知
            return;
        }
        // 获得键盘尺寸
        NSDictionary* info = [notif userInfo];
        NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;
        
        //重新定义ScrollView的尺寸
        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 *)notif {
        
        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;
        
        if (!keyboardVisible) {
            return;
        }
        
        keyboardVisible = NO;
        
    }
  • 相关阅读:
    liunx 学习
    Tracert 命令使用说明图解
    好的程序员应该收集一些不错的 类和方法
    apache 多端口
    数组中随机抽取一个或多个单元 (0086一随机显示列表)
    PHP 应具备的知识 学习
    rdlc报表中不显示0
    教程:VS2010 之TFS入门指南
    ORA00161: 事务处理的分支长度 xx 非法 (允许的最大长度为 64) 解决方法
    DataGridView编辑
  • 原文地址:https://www.cnblogs.com/cqchen/p/3764653.html
Copyright © 2011-2022 走看看