91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2020-09-28 00:07:34 來源:腳本之家 閱讀:627 作者:鴻鵠當高遠 欄目:移動開發

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

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

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

大致思路是提前創建好工具欄,在鍵盤彈出的時候將工具欄顯示出來,在鍵盤消失的時候讓工具欄隱藏

上代碼

設置兩個變量

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

創建工具欄 輸入框 添加鍵盤彈出 消失的通知

- (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]; 
 //增加監聽,當鍵盤出現或改變時收出消息 
 [[NSNotificationCenter defaultCenter] addObserver:self 
            selector:@selector(keyboardWillShow:) 
             name:UIKeyboardWillShowNotification 
            object:nil]; 
 //增加監聽,當鍵退出時收出消息 
 [[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]; 
} 

實現鍵盤通知的方法

#pragma mark 當鍵盤出現或改變時調用 
- (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 當鍵退出時調用 
- (void)keyboardWillHide:(NSNotification *)aNotification 
{ 
 //鍵盤消失時 隱藏工具欄 
 [UIView animateWithDuration:0.1 animations:^{ 
  _toolView.frame = CGRectMake(0, screen_Height+50, screen_Width, 50); 
 }]; 
} 

給工具欄上的各個按鈕實現點擊事件

- (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;

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

肃南| 讷河市| 十堰市| 北票市| 榕江县| 布拖县| 丁青县| 西乡县| 广南县| 大新县| 中西区| 江城| 舟曲县| 凯里市| 神农架林区| 习水县| 建宁县| 图木舒克市| 南城县| 石门县| 荆门市| 滁州市| 莱西市| 长岭县| 广州市| 石台县| 敖汉旗| 阜宁县| 阳城县| 温宿县| 连州市| 龙游县| 喀喇沁旗| 读书| 泸州市| 阜新市| 安多县| 舒兰市| 六枝特区| 克山县| 黑水县|