iOS中
.h
//
// ViewController.h
// 使用contentOffset让你的用户注册页面动起来
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scr;
@property (weak, nonatomic) IBOutlet UITextField *fd1;
@property (weak, nonatomic) IBOutlet UITextField *fd2;
@property (weak, nonatomic) IBOutlet UITextField *fd3;
@property (weak, nonatomic) IBOutlet UITextField *fd4;
@property (weak, nonatomic) IBOutlet UITextField *fd5;
@end
.m
//
// ViewController.m
// 使用contentOffset让你的用户注册页面动起来
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_fd1.delegate = self;_fd2.delegate = self;_fd3.delegate = self;_fd4.delegate = self;_fd5.delegate = self;// Do any additional setup after loading the view, typically from a nib.//按照我们nib上的输入框如果点击的话那么弹出来的键盘会遮盖住输入框,那么我们就可以用setContentOffset这个属性来解决这个问题}//在键盘将要输入的时候来判断
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{if (textField == _fd1) {[_scr setContentOffset:CGPointMake(0, 40) animated:YES];//这样我们让滑动视图向上弹起40的高度,那么键盘就无法遮住输入框了}else if (textField == _fd2) {[_scr setContentOffset:CGPointMake(0, 70) animated:YES];}else if (textField == _fd3) {[_scr setContentOffset:CGPointMake(0, 100) animated:YES];}else if (textField == _fd4) {[_scr setContentOffset:CGPointMake(0, 130) animated:YES];}else if (textField == _fd5) {[_scr setContentOffset:CGPointMake(0, 160) animated:YES];}//这样来我们会做出一个视图滚动的效果,对用户体验是比较好的return YES;//记得在键盘收回的时候给_scr还原
// [_scr setContentOffset:CGPointMake(0, 0) animated:YES];
}- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];// Dispose of any resources that can be recreated.
}@end
在swift中
//
// ViewController.swift
// 使用contentOffset让你的用户注册页面动起来
//
// Created by 黄权浩 on 15-1-4.
// Copyright (c) 2015年 黄权浩. All rights reserved.
//import UIKitclass ViewController: UIViewController,UITextFieldDelegate {@IBOutlet weak var scr: UIScrollView!@IBOutlet weak var fd1: UITextField!@IBOutlet weak var fd2: UITextField!@IBOutlet weak var fd3: UITextField!@IBOutlet weak var fd4: UITextField!@IBOutlet weak var fd5: UITextField!override func viewDidLoad() {super.viewDidLoad()// Do any additional setup after loading the view, typically from a nib.fd1.delegate = selffd2.delegate = selffd3.delegate = selffd4.delegate = selffd5.delegate = self//按照我们nib上的输入框如果点击的话那么弹出来的键盘会遮盖住输入框,那么我们就可以用setContentOffset这个属性来解决这个问题}//TextFidldDelegate代理func textFieldShouldBeginEditing(textField: UITextField) -> Bool{if textField == fd1 {scr .setContentOffset(CGPointMake(0, 40), animated: true)//这样我们让滑动视图向上弹起40的高度,那么键盘就无法遮住输入框了}else if textField == fd2 {scr .setContentOffset(CGPointMake(0, 70), animated: true)}else if textField == fd3 {scr .setContentOffset(CGPointMake(0, 100), animated: true)}else if textField == fd4 {scr .setContentOffset(CGPointMake(0, 130), animated: true)}else if textField == fd5 {scr .setContentOffset(CGPointMake(0, 160), animated: true)}//这样来我们会做出一个视图滚动的效果,对用户体验是比较好的return true;//记得在键盘收回的时候给_scr还原// [_scr setContentOffset:CGPointMake(0, 0) animated:YES];}override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.}}