Iphone在ScrollView下點擊TextField使文本筐不被鍵盤遮住

1.拖一個Scroll View視圖填充View窗口,將Scroll View視圖拖大一些,使其超出屏幕。

2.向Scroll View拖(添加)多個Label視圖和Text View視圖。

3.在.h頭文件中添加如下代碼:

[cpp] view plaincopyprint?
  1. #import?<UIKit/UIKit.h> ??
  2. ??
  3. ??
  4. @interface?ShowTextFiled?:?UIViewController?{??
  5. ????IBOutlet?UIScrollView?*myscrollview;??
  6. ??????
  7. ????UITextField?*?currentTextfield;//當前的文本筐 ??
  8. ????BOOL?keyboardIsShown;??
  9. }??
  10. @property?(nonatomic,retain)UIScrollView?*myscrollview;??
  11. ??
  12. @end??
#import <UIKit/UIKit.h>
@interface ShowTextFiled : UIViewController {
IBOutlet UIScrollView *myscrollview;
UITextField * currentTextfield;//當前的文本筐
BOOL keyboardIsShown;
}
@property (nonatomic,retain)UIScrollView *myscrollview;
@end

4.在Interface Builer中,將每個Text Field的delegate連接到File‘s Owner。這一步很重要,因為它使得文本筐的各種事件如:textFieldDidBeginEditing。textFieldDidEndEditing等可被試圖控制器處理。

5.在viewDidLoad方法里面修改ScrollView的內容大小:

[cpp] view plaincopyprint?
  1. -?(void)viewDidLoad??
  2. {??
  3. ????//下面這兩句話必須寫,否則scrollview不可以動 ??
  4. ????myscrollview.frame?=?CGRectMake(0,?0,?320,?460);??
  5. ????[myscrollview?setContentSize:CGSizeMake(320,651?)];??
  6. ????[super?viewDidLoad];??
  7. ????//?Do?any?additional?setup?after?loading?the?view?from?its?nib. ??
  8. }??
- (void)viewDidLoad
{
//下面這兩句話必須寫,否則scrollview不可以動
myscrollview.frame = CGRectMake(0, 0, 320, 460);
[myscrollview setContentSize:CGSizeMake(320,651 )];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

6.在View窗口顯示以前注冊兩個通知,這兩個通知可以告訴你鍵盤何時顯示或消失,在viewWillAppear方法里面注冊:

[cpp] view plaincopyprint?
  1. //頁面加載前調用的方法,注冊兩個通知:一個是鍵盤彈出來的通知,另外一個是鍵盤隱藏的通知,不同的通知調用不同的方法進行處理 ??
  2. -(void)?viewWillAppear:(BOOL)animated{??
  3. ????//鍵盤彈起的通知 ??
  4. ????[[NSNotificationCenter?defaultCenter]???
  5. ?????addObserver:self??
  6. ?????selector:@selector(keyboardDidShow:)???
  7. ?????name:UIKeyboardDidShowNotification??
  8. ?????object:self.view.window];??
  9. ????//鍵盤隱藏的通知 ??
  10. ????[[NSNotificationCenter?defaultCenter]??
  11. ?????addObserver:self??
  12. ?????selector:@selector(keyboardDidHide:)???
  13. ?????name:UIKeyboardDidHideNotification???
  14. ?????object:nil];??
  15. }??
//頁面加載前調用的方法,注冊兩個通知:一個是鍵盤彈出來的通知,另外一個是鍵盤隱藏的通知,不同的通知調用不同的方法進行處理
-(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];
}

7.上面的通知中,當鍵盤彈起的時候會調用 keyboardDidShow方法,當鍵盤消失會調用keyboardDidHide方法,這兩個方法定義如下:

[cpp] view plaincopyprint?
  1. //鍵盤彈起后處理scrollView的高度使得textfield可見 ??
  2. -(void)keyboardDidShow:(NSNotification?*)notification{??
  3. ????if?(keyboardIsShown)?{??
  4. ????????return;??
  5. ????}??
  6. ????NSDictionary?*?info?=?[notification?userInfo];??
  7. ????NSValue?*avalue?=?[info?objectForKey:UIKeyboardFrameEndUserInfoKey];??
  8. ????CGRect?keyboardRect?=?[self.view?convertRect:[avalue?CGRectValue]?fromView:nil];??
  9. ????CGRect?viewFrame?=?[myscrollview?frame];??
  10. ????viewFrame.size.height?-=?keyboardRect.size.height;??
  11. ????myscrollview.frame?=?viewFrame;??
  12. ????CGRect?textFieldRect?=?[currentTextfield?frame];??
  13. ????[myscrollview?scrollRectToVisible:textFieldRect?animated:YES];??
  14. ????keyboardIsShown?=?YES;??
  15. }??
//鍵盤彈起后處理scrollView的高度使得textfield可見
-(void)keyboardDidShow:(NSNotification *)notification{
if (keyboardIsShown) {
return;
}
NSDictionary * info = [notification userInfo];
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
CGRect viewFrame = [myscrollview frame];
viewFrame.size.height -= keyboardRect.size.height;
myscrollview.frame = viewFrame;
CGRect textFieldRect = [currentTextfield frame];
[myscrollview scrollRectToVisible:textFieldRect animated:YES];
keyboardIsShown = YES;
}

[cpp] view plaincopyprint?
  1. //鍵盤隱藏后處理scrollview的高度,使其還原為本來的高度 ??
  2. -(void)keyboardDidHide:(NSNotification?*)notification{??
  3. ????NSDictionary?*info?=?[notification?userInfo];??
  4. ????NSValue?*avalue?=?[info?objectForKey:UIKeyboardFrameEndUserInfoKey];??
  5. ????CGRect?keyboardRect?=?[self.view?convertRect:[avalue?CGRectValue]?fromView:nil];??
  6. ????CGRect?viewFrame?=?[myscrollview?frame];??
  7. ????viewFrame.size.height?+=?keyboardRect.size.height;??
  8. ????myscrollview.frame?=?viewFrame;??
  9. ????keyboardIsShown?=?NO;??
  10. }??
//鍵盤隱藏后處理scrollview的高度,使其還原為本來的高度
-(void)keyboardDidHide:(NSNotification *)notification{
NSDictionary *info = [notification userInfo];
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
CGRect viewFrame = [myscrollview frame];
viewFrame.size.height += keyboardRect.size.height;
myscrollview.frame = viewFrame;
keyboardIsShown = NO;
}

8.重寫TextField的三個事件方法:

當點擊TextField視圖時會促發如下方法:這個方法會將當前點擊的文本筐賦值給currentTextField成員變量

[cpp] view plaincopyprint?
  1. -(void)textFieldDidBeginEditing:(UITextField?*)textFieldView{??
  2. ????currentTextfield?=?textFieldView;??
  3. }??
-(void)textFieldDidBeginEditing:(UITextField *)textFieldView{
currentTextfield = textFieldView;
}
當用戶點擊鍵盤中的Return鍵時,會促發如下方法:

[cpp] view plaincopyprint?
  1. -(BOOL)textFieldShouldReturn:(UITextField?*)textFieldView{??
  2. ????[textFieldView?resignFirstResponder];??
  3. ????return?NO;??
  4. }??
-(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{
[textFieldView resignFirstResponder];
return NO;
}

上一個方法中的resignFirstResponder會隱藏鍵盤,這樣會促發另外一個方法:這里將currentTextField設為nil

[cpp] view plaincopyprint?
  1. -(void)textFieldDidEndEditing:(UITextField?*)textFieldView{??
  2. ????currentTextfield?=?nil;??
  3. }??
-(void)textFieldDidEndEditing:(UITextField *)textFieldView{
currentTextfield = nil;
}

9.最后不要忘了在View窗口關閉之前移除前面注冊的通知:

[cpp] view plaincopyprint?
  1. //頁面消失前取消通知 ??
  2. -(void)viewWillDisappear:(BOOL)animated{??
  3. ????[[NSNotificationCenter?defaultCenter]??
  4. ?????removeObserver:self??
  5. ?????name:UIKeyboardDidShowNotification??
  6. ?????object:nil];??
  7. ??????
  8. ????[[NSNotificationCenter?defaultCenter]??
  9. ?????removeObserver:self??
  10. ?????name:UIKeyboardDidHideNotification??
  11. ?????object:nil];??
  12. }??
//頁面消失前取消通知
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardDidHideNotification
object:nil];
}



生命在于運動,先去打球,晚上回來在繼續,哈哈。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388350.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388350.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388350.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

兩高發布司法解釋 依法嚴懲涉地下錢莊犯罪

新華社北京1月31日電&#xff08;記者丁小溪&#xff09;最高人民法院、最高人民檢察院31日聯合發布關于辦理非法從事資金支付結算業務、非法買賣外匯刑事案件適用法律若干問題的解釋&#xff0c;旨在依法懲治非法從事資金支付結算業務、非法買賣外匯犯罪活動&#xff0c;維護金…

python 儀表盤_如何使用Python刮除儀表板

python 儀表盤Dashboard scraping is a useful skill to have when the only way to interact with the data you need is through a dashboard. We’re going to learn how to scrape data from a dashboard using the Selenium and Beautiful Soup packages in Python. The S…

VS2015 定時服務及控制端

一. 服務端 如下圖—新建項目—經典桌面—Windows服務—起名svrr2. 打到server1 改名為svrExecSqlInsert 右擊對應的設計界面&#xff0c;添加安裝服務目錄結構如圖 3. svrExecSqlInsert里有打到OnStart()方法開始寫代碼如下 /// <summary>/// 服務開啟操作/// </su…

css文件如何設置scss,Webpack - 如何將scss編譯成單獨的css文件?

2 個答案:答案 0 :(得分&#xff1a;3)這是我在嘗試將css編譯成單獨文件時使用的webpack.config.js文件|-- App|-- dist|-- src|-- css|-- header.css|-- sass|-- img|-- partials|-- _variables.scss|-- main.scss|--ts|-- tsconfig.json|-- user.ts|-- main.js|-- app.js|-- …

Iphone表視圖的簡單操作

1.創建一個Navigation—based—Application項目&#xff0c;這樣Interface Builder中會自動生成一個Table View&#xff0c;然后將Search Bar拖放到表示圖上&#xff0c;以我們要給表示圖添加搜索功能&#xff0c;不要忘記將Search Bar的delegate連接到File‘s Owner項&#xf…

PhantomJS的使用

PhantomJS安裝下載地址 配置環境變量 成功&#xff01; 轉載于:https://www.cnblogs.com/hankleo/p/9736323.html

aws emr 大數據分析_DataOps —使用AWS Lambda和Amazon EMR的全自動,低成本數據管道

aws emr 大數據分析Progression is continuous. Taking a flashback journey through my 25 years career in information technology, I have experienced several phases of progression and adaptation.進步是連續的。 在我25年的信息技術職業生涯中經歷了一次閃回之旅&…

21eval 函數

eval() 函數十分強大 ---- 將字符串 當成 有效的表達式 來求職 并 返回計算結果 1 # 基本的數學計算2 print(eval("1 1")) # 23 4 # 字符串重復5 print(eval("* * 5")) # *****6 7 # 將字符串轉換成列表8 print(eval("[1, 2, 3, 4]")) # [1,…

聯想r630服務器開啟虛擬化,整合虛擬化 聯想萬全R630服務器上市

虛擬化技術的突飛猛進&#xff0c;對運行虛擬化應用的服務器平臺的運算性能提出了更高的要求。近日&#xff0c;聯想萬全R630G7正式對外發布。這款計算性能強勁&#xff0c;IO吞吐能力強大的四路四核服務器&#xff0c;主要面向高端企業級應用而開發。不僅能夠完美承載大規模數…

Iphone屏幕旋轉

該示例是想在手機屏幕方向發生改變時重新定位視圖&#xff08;這里是一個button&#xff09; 1.創建一個View—based Application項目,并在View窗口中添加一個Round Rect Button視圖&#xff0c;通過尺寸檢查器設置其位置&#xff0c;然后單擊View窗口右上角的箭頭圖標來旋轉窗…

先進的NumPy數據科學

We will be covering some of the advanced concepts of NumPy specifically functions and methods required to work on a realtime dataset. Concepts covered here are more than enough to start your journey with data.我們將介紹NumPy的一些高級概念&#xff0c;特別是…

lsof命令詳解

基礎命令學習目錄首頁 原文鏈接&#xff1a;https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316599.html 簡介 lsof(list open files)是一個列出當前系統打開文件的工具。在linux環境下&#xff0c;任何事物都以文件的形式存在&#xff0c;通過文件不僅僅可以訪問常規…

Xcode中捕獲iphone/ipad/ipod手機攝像頭的實時視頻數據

目的&#xff1a;打開、關閉前置攝像頭&#xff0c;繪制圖像&#xff0c;并獲取攝像頭的二進制數據。 需要的庫 AVFoundation.framework 、CoreVideo.framework 、CoreMedia.framework 、QuartzCore.framework 該攝像頭捕抓必須編譯真機的版本&#xff0c;模擬器下編譯不了。 函…

統計和冰淇淋

Photo by Irene Kredenets on UnsplashIrene Kredenets在Unsplash上拍攝的照片 摘要 (Summary) In this article, you will learn a little bit about probability calculations in R Studio. As it is a Statistical language, R comes with many tests already built in it, …

信息流服務器哪種好,選購存儲服務器需要注意六大關鍵因素,你知道幾個?

原標題&#xff1a;選購存儲服務器需要注意六大關鍵因素&#xff0c;你知道幾個&#xff1f;信息技術的飛速發展帶動了整個信息產業的發展。越來越多的電子商務平臺和虛擬化環境出現在企業的日常應用中。存儲服務器作為企業建設環境的核心設備&#xff0c;在整個信息流中承擔著…

t3 深入Tornado

3.1 Application settings 前面的學習中&#xff0c;在創建tornado.web.Application的對象時&#xff0c;傳入了第一個參數——路由映射列表。實際上Application類的構造函數還接收很多關于tornado web應用的配置參數。 參數&#xff1a; debug&#xff0c;設置tornado是否工作…

vml編輯器

<HTML xmlns:v> <HEAD> <META http-equiv"Content-Type" content"text/html; Charsetgb2312"> <META name"GENERATOR" content"網絡程序員伴侶(Lshdic)2004"> <META name"GENERATORDOWNLOADADDRESS&q…

對數據倉庫進行數據建模_確定是否可以對您的數據進行建模

對數據倉庫進行數據建模Some data sets are just not meant to have the geospatial representation that can be clustered. There is great variance in your features, and theoretically great features as well. But, it doesn’t mean is statistically separable.某些數…

15 并發編程-(IO模型)

一、IO模型介紹 1、阻塞與非阻塞指的是程序的兩種運行狀態 阻塞&#xff1a;遇到IO就發生阻塞&#xff0c;程序一旦遇到阻塞操作就會停在原地&#xff0c;并且立刻釋放CPU資源 非阻塞&#xff08;就緒態或運行態&#xff09;&#xff1a;沒有遇到IO操作&#xff0c;或者通過某種…

arduino消息服務器,在C(Arduino IDE)中將API鏈接消息解析為服務器(示例代碼)

我正在使用Arduino IDE來編程我的微控制器&#xff0c;它有一個內置的Wi-Fi芯片(ESP8266 NodeMCU)&#xff0c;它連接到我的互聯網路由器&#xff0c;然后有一個特定的IP(就像192.168.1.5)。所以我想通過添加到鏈接的消息發送命令(和數據)&#xff0c;然后鏈接變為&#xff1a;…