上面的按鈕直接給上點擊方式就可以了,如有不懂可以交流討論QQ號:1466534942 時過境遷
#import "ViewController.h"
@interface ViewController ()
{
UIView * _toolView; //工具欄
UITextField *textField;// 輸入框 呼出鍵
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 150, 120, 70)];
textField.placeholder = @"測試";
textField.keyboardType = UIKeyboardTypeNumberPad;
textField.backgroundColor =[UIColor redColor];
[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.backgroundColor =[UIColor grayColor];
_toolView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 50);
[self.view addSubview:_toolView];
UIButton *losebtn = [UIButton buttonWithType:UIButtonTypeCustom];
losebtn.frame = CGRectMake(50, 0, 100, 50);
[losebtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[losebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[losebtn setTitle:@"+86 China" forState:UIControlStateNormal];
[_toolView addSubview:losebtn];
UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[imageBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
imageBtn.frame = CGRectMake(self.view.frame.size.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(self.view.frame.size.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(self.view.frame.size.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, 400, self.view.frame.size.width, 50);
}];
}
#pragma mark 當鍵退出時調用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
//鍵盤消失時 隱藏工具欄
[UIView animateWithDuration:0.1 animations:^{
_toolView.frame = CGRectMake(0, self.view.frame.size.height+10, 100, 50);
}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[textField resignFirstResponder];
}
@end
自定義鍵盤.gif