美文网首页
🌺自定义键盘上方工具栏🌺

🌺自定义键盘上方工具栏🌺

作者: JaneEyre3X | 来源:发表于2018-10-02 23:44 被阅读0次

上面的按钮直接给上点击方式就可以了,如有不懂可以交流讨论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

相关文章

  • 🌺自定义键盘上方工具栏🌺

    上面的按钮直接给上点击方式就可以了,如有不懂可以交流讨论QQ号:1466534942 时过境迁

  • 语音搜索功能

    首先,我们需要自定义一个textField 用于绑定语音搜索的功能。自定义键盘就没做了,只是简单的在键盘上方添加了...

  • 【Axure10】菜单-视图

    工具栏 主工具栏 主工具栏是否展示,默认展示。 自定义工具栏 自定义主工具栏功能组件。 注:可以结合快捷键与常用功...

  • iOS自定义键盘弹出view的位置移动(三种方法)

    当弹出键盘时,自定义键盘上方的view,有三种解决办法:一个就是利用UITextField或者UITextView...

  • Swift 设置UIDatePicker键盘输入日期

    1、声明datePicker属性 2、设置输入框的键盘为datePicker 3、设置键盘的工具栏 4、添加工具栏...

  • WM 基础

    主界面 上方: 菜单及工具栏 下方左侧: 模板管理和效果查看 下方右侧: 主编辑界面 上方: 不同功能模块的工具栏...

  • 自定义键盘上方工具栏随键盘一起运动的问题

    监听键盘变化的通知。有改变的,显示的,隐藏的几个系统通知。 这么处理,感觉跟随键盘一起走比较自然一些。显示和隐藏的...

  • macOS开发-NSToolBar

    NSToolBar 1 简述 工具栏,用于管理窗口标题栏下方和应用程序的自定义内容上方的空间,以快速访问应用程序功...

  • 自定义键盘工具栏——KeyboardToolBar

    今天项目中需要给键盘加上一个工具栏,功能为实现上一步,下一步的textfield的切换和点击“完成”收起键盘,功能...

  • PL/SQL 导出表结构

    工具栏上方选择Tools(工具)--> Export User Objects(导出用户对象)工具栏 选择要导出的...

网友评论

      本文标题:🌺自定义键盘上方工具栏🌺

      本文链接:https://www.haomeiwen.com/subject/sptzoftx.html