zoukankan      html  css  js  c++  java
  • 快速解决键盘遮挡问题

    • 自适应键盘出现后View的高度调整,防止遮挡输入框

    1.首先在LoginViewController中实现UITextField的一个Delegate

    //  Created by ken on 13-7-21.

    //  Copyright (c) 2013年 ken. All rights reserved.

    //

     

    #import <UIKit/UIKit.h>

     

    @interface LoginViewController : UIViewController<UITextFieldDelegate>

     

     

    @property (retain, nonatomic) IBOutlet UITextField *userNumber;

     

    @property (retain, nonatomic) IBOutlet UITextField *userPassword;

     

    2.实现UITextFiledDelegate中的协议方法

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

     

        self.userNumber.delegate = self;

        self.userPassword.delegate = self;

     

    }

     

     

    //UITextField的协议方法,当开始编辑时监听

    -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField

    {

        NSTimeInterval animationDuration=0.30f;

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

        [UIView setAnimationDuration:animationDuration];

        float width = self.view.frame.size.width;

        float height = self.view.frame.size.height;

        //上移30个单位,按实际情况设置

        CGRect rect=CGRectMake(0.0f,-30,width,height);

        self.view.frame=rect;

        [UIView commitAnimations];

        return YES;

    }

    //UITextField的协议方法,当结束编辑时监听

    - (void) textFieldDidEndEditing:(UITextField *)textField{

        [self resumeView];

    }

     

    //恢复原始视图位置

    -(void)resumeView

    {

        NSTimeInterval animationDuration=0.30f;

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

        [UIView setAnimationDuration:animationDuration];

        float width = self.view.frame.size.width;

        float height = self.view.frame.size.height;

        //如果当前View是父视图,则Y为20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度

        float Y = 20.0f;

        CGRect rect=CGRectMake(0.0f,Y,width,height);

        self.view.frame=rect;

        [UIView commitAnimations];

    }

  • 相关阅读:
    HDU 4320 Arcane Numbers 1(质因子包含)
    BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)
    Codeforces Beta Round #65 (Div. 2) C. Round Table Knights
    HDU 4496 D-City(逆向并查集)
    HDU 3047 Zjnu Stadium(带权并查集)
    HDU 4104 Discount(n个数不能构成的最小值)
    hihoCoder 1515 分数调查(带权并查集)
    POJ 1733 Parity game(种类并查集)
    Codeforces Round #107 (Div. 1) B. Quantity of Strings(推算)
    CSU 2005 Nearest Maintenance Point(最短路+bitset)
  • 原文地址:https://www.cnblogs.com/angongIT/p/4174579.html
Copyright © 2011-2022 走看看