日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Iphone在ScrollView下点击TextField使文本筐不被键盘遮住

發布時間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Iphone在ScrollView下点击TextField使文本筐不被键盘遮住 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

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

[cpp] view plaincopyprint?
  • #import?<UIKit/UIKit.h> ??
  • ??
  • ??
  • @interface?ShowTextFiled?:?UIViewController?{??
  • ????IBOutlet?UIScrollView?*myscrollview;??
  • ??????
  • ????UITextField?*?currentTextfield;//當前的文本筐 ??
  • ????BOOL?keyboardIsShown;??
  • }??
  • @property?(nonatomic,retain)UIScrollView?*myscrollview;??
  • ??
  • @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?
  • -?(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. ??
  • }??
  • - (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?
  • //頁面加載前調用的方法,注冊兩個通知:一個是鍵盤彈出來的通知,另外一個是鍵盤隱藏的通知,不同的通知調用不同的方法進行處理 ??
  • -(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];??
  • }??
  • //頁面加載前調用的方法,注冊兩個通知:一個是鍵盤彈出來的通知,另外一個是鍵盤隱藏的通知,不同的通知調用不同的方法進行處理 -(void) viewWillAppear:(BOOL)animated{//鍵盤彈起的通知[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotificationobject:self.view.window];//鍵盤隱藏的通知[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; }
    7.上面的通知中,當鍵盤彈起的時候會調用 keyboardDidShow方法,當鍵盤消失會調用keyboardDidHide方法,這兩個方法定義如下:

    [cpp] view plaincopyprint?
  • //鍵盤彈起后處理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;??
  • }??
  • //鍵盤彈起后處理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?
  • //鍵盤隱藏后處理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;??
  • }??
  • //鍵盤隱藏后處理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?
  • -(void)textFieldDidBeginEditing:(UITextField?*)textFieldView{??
  • ????currentTextfield?=?textFieldView;??
  • }??
  • -(void)textFieldDidBeginEditing:(UITextField *)textFieldView{currentTextfield = textFieldView; }當用戶點擊鍵盤中的Return鍵時,會促發如下方法:

    [cpp] view plaincopyprint?
  • -(BOOL)textFieldShouldReturn:(UITextField?*)textFieldView{??
  • ????[textFieldView?resignFirstResponder];??
  • ????return?NO;??
  • }??
  • -(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{[textFieldView resignFirstResponder];return NO; }
    上一個方法中的resignFirstResponder會隱藏鍵盤,這樣會促發另外一個方法:這里將currentTextField設為nil

    [cpp] view plaincopyprint?
  • -(void)textFieldDidEndEditing:(UITextField?*)textFieldView{??
  • ????currentTextfield?=?nil;??
  • }??
  • -(void)textFieldDidEndEditing:(UITextField *)textFieldView{currentTextfield = nil; }
    9.最后不要忘了在View窗口關閉之前移除前面注冊的通知:

    [cpp] view plaincopyprint?
  • //頁面消失前取消通知 ??
  • -(void)viewWillDisappear:(BOOL)animated{??
  • ????[[NSNotificationCenter?defaultCenter]??
  • ?????removeObserver:self??
  • ?????name:UIKeyboardDidShowNotification??
  • ?????object:nil];??
  • ??????
  • ????[[NSNotificationCenter?defaultCenter]??
  • ?????removeObserver:self??
  • ?????name:UIKeyboardDidHideNotification??
  • ?????object:nil];??
  • }??
  • //頁面消失前取消通知 -(void)viewWillDisappear:(BOOL)animated{[[NSNotificationCenter defaultCenter]removeObserver:selfname:UIKeyboardDidShowNotificationobject:nil];[[NSNotificationCenter defaultCenter]removeObserver:selfname:UIKeyboardDidHideNotificationobject:nil]; }


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

    總結

    以上是生活随笔為你收集整理的Iphone在ScrollView下点击TextField使文本筐不被键盘遮住的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。