日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

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

發(fā)布時(shí)間:2023/11/30 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Iphone在ScrollView下点击TextField使文本筐不被键盘遮住 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

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

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

[cpp] view plaincopyprint?
  • #import?<UIKit/UIKit.h> ??
  • ??
  • ??
  • @interface?ShowTextFiled?:?UIViewController?{??
  • ????IBOutlet?UIScrollView?*myscrollview;??
  • ??????
  • ????UITextField?*?currentTextfield;//當(dāng)前的文本筐 ??
  • ????BOOL?keyboardIsShown;??
  • }??
  • @property?(nonatomic,retain)UIScrollView?*myscrollview;??
  • ??
  • @end??
  • #import <UIKit/UIKit.h>@interface ShowTextFiled : UIViewController {IBOutlet UIScrollView *myscrollview;UITextField * currentTextfield;//當(dāng)前的文本筐B(yǎng)OOL keyboardIsShown; } @property (nonatomic,retain)UIScrollView *myscrollview;@end
    4.在Interface Builer中,將每個(gè)Text Field的delegate連接到File‘s Owner。這一步很重要,因?yàn)樗沟梦谋究鸬母鞣N事件如:textFieldDidBeginEditing。textFieldDidEndEditing等可被試圖控制器處理。

    5.在viewDidLoad方法里面修改ScrollView的內(nèi)容大小:

    [cpp] view plaincopyprint?
  • -?(void)viewDidLoad??
  • {??
  • ????//下面這兩句話必須寫(xiě),否則scrollview不可以動(dòng) ??
  • ????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 {//下面這兩句話必須寫(xiě),否則scrollview不可以動(dòng)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窗口顯示以前注冊(cè)兩個(gè)通知,這兩個(gè)通知可以告訴你鍵盤(pán)何時(shí)顯示或消失,在viewWillAppear方法里面注冊(cè):

    [cpp] view plaincopyprint?
  • //頁(yè)面加載前調(diào)用的方法,注冊(cè)兩個(gè)通知:一個(gè)是鍵盤(pán)彈出來(lái)的通知,另外一個(gè)是鍵盤(pán)隱藏的通知,不同的通知調(diào)用不同的方法進(jìn)行處理 ??
  • -(void)?viewWillAppear:(BOOL)animated{??
  • ????//鍵盤(pán)彈起的通知 ??
  • ????[[NSNotificationCenter?defaultCenter]???
  • ?????addObserver:self??
  • ?????selector:@selector(keyboardDidShow:)???
  • ?????name:UIKeyboardDidShowNotification??
  • ?????object:self.view.window];??
  • ????//鍵盤(pán)隱藏的通知 ??
  • ????[[NSNotificationCenter?defaultCenter]??
  • ?????addObserver:self??
  • ?????selector:@selector(keyboardDidHide:)???
  • ?????name:UIKeyboardDidHideNotification???
  • ?????object:nil];??
  • }??
  • //頁(yè)面加載前調(diào)用的方法,注冊(cè)兩個(gè)通知:一個(gè)是鍵盤(pán)彈出來(lái)的通知,另外一個(gè)是鍵盤(pán)隱藏的通知,不同的通知調(diào)用不同的方法進(jìn)行處理 -(void) viewWillAppear:(BOOL)animated{//鍵盤(pán)彈起的通知[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotificationobject:self.view.window];//鍵盤(pán)隱藏的通知[[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; }
    7.上面的通知中,當(dāng)鍵盤(pán)彈起的時(shí)候會(huì)調(diào)用 keyboardDidShow方法,當(dāng)鍵盤(pán)消失會(huì)調(diào)用keyboardDidHide方法,這兩個(gè)方法定義如下:

    [cpp] view plaincopyprint?
  • //鍵盤(pán)彈起后處理scrollView的高度使得textfield可見(jiàn) ??
  • -(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;??
  • }??
  • //鍵盤(pán)彈起后處理scrollView的高度使得textfield可見(jiàn) -(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?
  • //鍵盤(pán)隱藏后處理scrollview的高度,使其還原為本來(lái)的高度 ??
  • -(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;??
  • }??
  • //鍵盤(pán)隱藏后處理scrollview的高度,使其還原為本來(lái)的高度 -(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.重寫(xiě)TextField的三個(gè)事件方法:

    當(dāng)點(diǎn)擊TextField視圖時(shí)會(huì)促發(fā)如下方法:這個(gè)方法會(huì)將當(dāng)前點(diǎn)擊的文本筐賦值給currentTextField成員變量

    [cpp] view plaincopyprint?
  • -(void)textFieldDidBeginEditing:(UITextField?*)textFieldView{??
  • ????currentTextfield?=?textFieldView;??
  • }??
  • -(void)textFieldDidBeginEditing:(UITextField *)textFieldView{currentTextfield = textFieldView; }當(dāng)用戶點(diǎn)擊鍵盤(pán)中的Return鍵時(shí),會(huì)促發(fā)如下方法:

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

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

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


    生命在于運(yùn)動(dòng),先去打球,晚上回來(lái)在繼續(xù),哈哈。

    總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。