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;

}

iOS.UIKit.05.UIScrollView,布布扣,bubuko.com

时间: 2024-10-05 06:01:47

iOS.UIKit.05.UIScrollView的相关文章

iOS.UIKit.12.UICollectionView

一.案例介绍:以集合形式展现图片,如图01 图01 二.案例步骤: 1.选择Simple View Aplication,取名cq.38.集合视图,如图02 图02 2.Main.storyboard,如图03 图03 3.events.plist,如图04,05 4.CQ38ViewController.h #import <UIKit/UIKit.h> @interface CQ38ViewController : UICollectionViewController @property

iOS.UIKit.09.UINavigationBar

一.点击UINavigationBar中的UIBarButtonItem,改变UILabel中的文本,如图01,02,03 图01图02图03 二.项目步骤: 1.选择Simple View Aplication,取名cq.40.UINavigationBar,如图04 图04 2.Main.storyboard,如图05 3.CQ40ViewController.h #import <UIKit/UIKit.h> @interface CQ40ViewController : UIViewC

iOS.UIKit.08.UIToolbar

一.案例介绍:点击UIToolbar中的UIBarButtonItem,改变UILabel中的文本,如图01,02,03 图01图02图03 二.案例步骤: 1.选择Simple View Aplication,取名cq.33.工具栏和导航栏,如图04 2.Maim.storyboard,如图05 3.CQ33ViewController.h #import <UIKit/UIKit.h> @interface CQ33ViewController : UIViewController @pr

iOS.UIKit.06.UIProgressView_UIActivityIndicatorView

一.案例介绍:点击按钮upload,活动指示器转动,再次点击停止转动:点击download按钮进度条加载满后提示,如图01,图02. 图01图02 二.案例步骤: 1.选择Simple View Aplication,取名cq.31.活动指示器和进度条,如图03. 图03 2.Main.storyboard,如图04,图05. 图04图05 3.CQ31ViewController.h #import <UIKit/UIKit.h> @interface CQ31ViewController

iOS UIKit 框架 346 篇文档分类整理 - 预告

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. 当前正在进行的是 "iOS Foundation 框架 224 篇相关文档分类整理",量很大,但会根据实际开发中的使用频繁程序

iOS 开发 之 - UIScrollView文档

额.这篇文档主银:http://blog.sina.com.cn/u/2141630575  #import <Foundation/Foundation.h> #import <CoreGraphics/CoreGraphics.h> #import <UIKit/UIView.h> #import <UIKit/UIGeometry.h> #import <UIKit/UIKitDefines.h> typedef enum { UIScro

iOS.UIKit.11.UIPickerView

一.案例介绍:点击UIButton,将选中的省市信息显示在UILabel中,如图01,02 图01图02 二.案例步骤: 1.选择Simple View Aplication,取名cq.37.普通选择器,如图03 图03 2.Main.storyboard,如图04 3.provinces_cities.plist 4.CQ37ViewController.h #import <UIKit/UIKit.h> @interface CQ37ViewController : UIViewContr

iOS.UIKit.10.UIDatePicker

一.案例介绍:点击屏幕中的UIButton,将在UIDatePicker选择的日期显示在UILabel中,如图01,02 图01图02 二.案例步骤: 1.选择Simple View Aplication,取名cq.36.日期选择器,如图03 图03 2.Main.storyboard,如图04 3.CQ36ViewController.h #import <UIKit/UIKit.h> @interface CQ36ViewController : UIViewController @pro

iOS.UIKit.07.UIAlertView_UIActionSheet

一.案例介绍:点击第一个按钮弹出提示框:点击第二个按钮弹出操作表.如图01,图02,图03 图01图02图03 二.案例步骤: 1.选择Simple View Aplication,取名cq.32.警告框和操作表,如图04 2.Main.storyboard 3.CQ32ViewController.h #import <UIKit/UIKit.h> @interface CQ32ViewController : UIViewController<UIAlertViewDelegate,