iOS中只讓textField使用鍵盤通知的實例代碼-創(chuàng)新互聯(lián)

代碼:

網(wǎng)站建設公司,為您提供網(wǎng)站建設,網(wǎng)站制作,網(wǎng)頁設計及定制網(wǎng)站建設服務,專注于成都定制網(wǎng)頁設計,高端網(wǎng)頁制作,對成都公路鉆孔機等多個行業(yè)擁有豐富的網(wǎng)站建設經(jīng)驗的網(wǎng)站建設公司。專業(yè)網(wǎng)站設計,網(wǎng)站優(yōu)化推廣哪家好,專業(yè)成都網(wǎng)站推廣優(yōu)化,H5建站,響應式網(wǎng)站。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  //為textField增加鍵盤事件
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addKeyboardNoti) name:UITextFieldTextDidBeginEditingNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeKeyboardNoti) name:UITextFieldTextDidEndEditingNotification object:nil];
}
#pragma -mark -keyboard notificatin
//鍵盤事件
- (void)keyboardWillShow:(NSNotification *)notification {
  NSDictionary *info = [notification userInfo];
  // keyboardHeight 為鍵盤高度
  CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
  [self animateViewWithKeyboardHeight:keyboardSize.height];
}
- (void)keyboardWillHide:(NSNotification *)notification {
  [self animateViewWithKeyboardHeight:0.0];
}
- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight {
  NSTimeInterval animationDuration = 0.3f;
  CGFloat height = self.view.bounds.size.height;
  CGFloat width = self.view.bounds.size.width;
  CGFloat topSize = 0.0;
  CGFloat viewH = self.view.frame.size.height-64;
  CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height;
  CGFloat animateH = deviceHeight - viewH - keyboardHeight;
  if (animateH >= 0) {
    topSize = 0;
    CGRect toRect = CGRectMake(0, topSize, width, height);
    self.view.frame = toRect;
  } else {
    topSize = animateH;
    CGRect toRect = CGRectMake(0, topSize, width, height);
    [UIView animateWithDuration:animationDuration animations:^{
      self.view.frame = toRect;
    }];
  }
}
#pragma -mark -UITextFieldText Notification
//增加鍵盤事件
-(void)addKeyboardNoti
{
  NSLog(@"------addKeyboardNoti-------");
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
//移除鍵盤事件
-(void)removeKeyboardNoti
{
  NSLog(@"------removeKeyboardNoti---------");
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}
@end

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞名稱:iOS中只讓textField使用鍵盤通知的實例代碼-創(chuàng)新互聯(lián)
當前URL:http://www.muchs.cn/article38/dgiipp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、外貿(mào)建站、網(wǎng)站收錄網(wǎng)站改版、企業(yè)建站搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站