最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS中鍵盤 KeyBoard 上添加工具欄的方法

 更新時間:2017年08月10日 10:47:18   作者:鴻鵠當高遠  
大iOS中 鍵盤 KeyBoard 上怎么添加工具欄呢?大致思路是提前創(chuàng)建好工具欄,在鍵盤彈出的時候?qū)⒐ぞ邫陲@示出來,在鍵盤消失的時候讓工具欄隱藏。具體實現(xiàn)代碼大家參考下本文吧

 iOS中 鍵盤 KeyBoard 上怎么添加工具欄?

如圖中所示 在鍵盤上面加一條工具欄

大致思路是提前創(chuàng)建好工具欄,在鍵盤彈出的時候?qū)⒐ぞ邫陲@示出來,在鍵盤消失的時候讓工具欄隱藏

上代碼

設(shè)置兩個變量

UIView * _toolView; //工具欄 
UITextField *textField;// 輸入框 呼出鍵盤用 

創(chuàng)建工具欄 輸入框 添加鍵盤彈出 消失的通知

- (void)viewDidLoad { 
 [super viewDidLoad]; 
 // Do any additional setup after loading the view, typically from a nib. 
 textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 64, 120, 60)]; 
 textField.placeholder = @"測試"; 
 [self.view addSubview:textField]; 
 //增加監(jiān)聽,當鍵盤出現(xiàn)或改變時收出消息 
 [[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(keyboardWillShow:) 
             name:UIKeyboardWillShowNotification 
            object:nil]; 
 //增加監(jiān)聽,當鍵退出時收出消息 
 [[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(keyboardWillHide:) 
             name:UIKeyboardWillHideNotification object:nil]; 
 //初始化工具欄 
 _toolView = [[UIView alloc]init]; 
 _toolView.frame = CGRectMake(0, screen_Height, screen_Width, 50); 
 [self.view addSubview:_toolView]; 
 UIButton *losebtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
 losebtn.frame = CGRectMake(20, 0, 50, 50); 
 [losebtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; 
 [losebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
 [losebtn setTitle:@"收起" forState:UIControlStateNormal]; 
 [_toolView addSubview:losebtn]; 
 UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
 [imageBtn setTitle:@"圖片" forState:UIControlStateNormal]; 
 imageBtn.frame = CGRectMake(screen_Width-100, 0, 50, 50); 
 [imageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
 [imageBtn addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside]; 
 [_toolView addSubview:imageBtn]; 
 UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
 [cameraBtn setTitle:@"相機" forState:UIControlStateNormal]; 
 cameraBtn.frame = CGRectMake(screen_Width-50, 0, 50, 50); 
 [cameraBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
 [cameraBtn addTarget:self action:@selector(cameraBtnClick) forControlEvents:UIControlEventTouchUpInside]; 
 [_toolView addSubview:cameraBtn]; 
 UIButton *canclebtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
 [canclebtn setTitle:@"取消" forState:UIControlStateNormal]; 
 canclebtn.frame = CGRectMake(screen_Width-150, 0, 50, 50); 
 [canclebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
 [canclebtn addTarget:self action:@selector(canclebtnBtnClick) forControlEvents:UIControlEventTouchUpInside]; 
 [_toolView addSubview:canclebtn]; 
} 

實現(xiàn)鍵盤通知的方法

#pragma mark 當鍵盤出現(xiàn)或改變時調(diào)用 
- (void)keyboardWillShow:(NSNotification *)aNotification 
{ 
 //鍵盤彈出時顯示工具欄 
 //獲取鍵盤的高度 
 NSDictionary *userInfo = [aNotification userInfo]; 
 NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
 CGRect keyboardRect = [aValue CGRectValue]; 
 float keyBoardHeight = keyboardRect.size.height; 
 // NSLog(@"%ld",(long)keyBoardHeight); 
 [UIView animateWithDuration:0.1 animations:^{ 
  _toolView.frame = CGRectMake(0, screen_Height-keyBoardHeight-50, screen_Width, 50); 
 }]; 
} 
#pragma mark 當鍵退出時調(diào)用 
- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
 //鍵盤消失時 隱藏工具欄 
 [UIView animateWithDuration:0.1 animations:^{ 
  _toolView.frame = CGRectMake(0, screen_Height+50, screen_Width, 50); 
 }]; 
} 

給工具欄上的各個按鈕實現(xiàn)點擊事件

- (void)btnClick{ 
 [textField resignFirstResponder]; 
} 
- (void)imageBtnClick{ 
} 
- (void)cameraBtnClick{ 
} 
- (void)canclebtnBtnClick{ 
} 

PS:下面看下iOS 鍵盤上方增加工具欄的代碼。

具體代碼如下所示:

UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                style:UIBarButtonItemStyleBordered target:self
                action:@selector(doneClicked:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
txtField.inputAccessoryView = keyboardDoneButtonView;

相關(guān)文章

  • iOS實現(xiàn)帶動畫的環(huán)形進度條

    iOS實現(xiàn)帶動畫的環(huán)形進度條

    這篇文章主要為大家詳細介紹了iOS實現(xiàn)帶動畫的環(huán)形進度條,同時帶數(shù)字同步效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • iOS tableView多輸入框如何獲取數(shù)據(jù)

    iOS tableView多輸入框如何獲取數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于iOS tableView多輸入框如何獲取數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • iOS使用UIBezierPath實現(xiàn)ProgressView

    iOS使用UIBezierPath實現(xiàn)ProgressView

    這篇文章主要為大家詳細介紹了iOS使用UIBezierPath實現(xiàn)ProgressView,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • 教你如何解決XCODE升級后插件不能用問題

    教你如何解決XCODE升級后插件不能用問題

    Xcode 每次更新有個很頭疼的問題,就是插件都會失效,要重裝。 不得不說好多插件還是非常方便能提高效率。那么如何來解決這個問題呢,今天我們就來探討下。
    2015-11-11
  • 詳解iOS設(shè)計中的UIWindow使用

    詳解iOS設(shè)計中的UIWindow使用

    這篇文章主要介紹了iOS設(shè)計中的UIWindow使用,包括UIWindowLevel和KeyWindow兩個大的方面的講解,需要的朋友可以參考下
    2015-09-09
  • iOS中多線程的經(jīng)典崩潰總結(jié)大全

    iOS中多線程的經(jīng)典崩潰總結(jié)大全

    這篇文章主要給大家介紹了關(guān)于iOS中多線程的一些經(jīng)典崩潰的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • iOS AVPlayer切換播放源實現(xiàn)連續(xù)播放和全屏切換的方法

    iOS AVPlayer切換播放源實現(xiàn)連續(xù)播放和全屏切換的方法

    這篇文章主要給大家介紹了關(guān)于iOS中AVPlayer切換播放源實現(xiàn)連續(xù)播放和全屏切換的方法,文中給出了詳細的示例代碼供大家參考學習,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • 舉例詳解iOS開發(fā)過程中的沙盒機制與文件

    舉例詳解iOS開發(fā)過程中的沙盒機制與文件

    這篇文章主要介紹了舉例詳解iOS開發(fā)過程中的沙盒機制與文件,示例代碼為傳統(tǒng)的Obejective-C,需要的朋友可以參考下
    2015-09-09
  • 分享一個iOS下實現(xiàn)基本繪畫板功能的簡單方法

    分享一個iOS下實現(xiàn)基本繪畫板功能的簡單方法

    這篇文章主要介紹了iOS下實現(xiàn)基本繪畫板功能的簡單方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-10-10
  • iOS13 適配和Xcode11.0踩坑小結(jié)

    iOS13 適配和Xcode11.0踩坑小結(jié)

    這篇文章主要介紹了iOS13 適配和Xcode11.0踩坑小結(jié),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11

最新評論

平舆县| 望谟县| 乐都县| 东阳市| 宿迁市| 邯郸市| 邛崃市| 西华县| 昌图县| 宝清县| 康乐县| 札达县| 塔河县| 汝城县| 化德县| 南漳县| 缙云县| 六枝特区| 平和县| 富宁县| 广灵县| 宁远县| 合水县| 寿光市| 大渡口区| 五寨县| 舟山市| 微山县| 洛浦县| 珲春市| 西充县| 吐鲁番市| 柘荣县| 金阳县| 琼中| 广饶县| 雅江县| 清镇市| 水城县| 灵台县| 正宁县|