IOS TextFile键盘不遮挡 + 下拉隐藏键盘

本文详细介绍了如何创建一个自定义键盘控件,包括UI组件布局、事件响应及键盘弹起时的视图调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

self.title = @"DAKeyboardControl";

    self.view.backgroundColor = [UIColor lightGrayColor];

    

    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f,

                                                                           0.0f,

                                                                           self.view.bounds.size.width,

                                                                           self.view.bounds.size.height - 40.0f)];

    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [self.view addSubview:tableView];

    

    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f,

                                                                     self.view.bounds.size.height - 40.0f,

                                                                     self.view.bounds.size.width,

                                                                     40.0f)];

    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

    [self.view addSubview:toolBar];

    

    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0f,

                                                                           6.0f,

                                                                           toolBar.bounds.size.width - 20.0f - 68.0f,

                                                                           30.0f)];

    textField.borderStyle = UITextBorderStyleRoundedRect;

    textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;

    [toolBar addSubview:textField];

    

    UIButton *sendButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    sendButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;

    [sendButton setTitle:@"Send" forState:UIControlStateNormal];

    sendButton.frame = CGRectMake(toolBar.bounds.size.width - 68.0f,

                                  6.0f,

                                  58.0f,

                                  29.0f);

    [toolBar addSubview:sendButton];

    

    

    self.view.keyboardTriggerOffset = toolBar.bounds.size.height;

    

    [self.view addKeyboardPanningWithActionHandler:^(CGRect keyboardFrameInView)

    {

        /* 

         Try not to call "self" inside this block (retain cycle).

         But if you do, make sure to remove DAKeyboardControl

         when you are done with the view controller by calling:

         [self.view removeKeyboardControl];

         */

        

        CGRect toolBarFrame = toolBar.frame;

        toolBarFrame.origin.y = keyboardFrameInView.origin.y - toolBarFrame.size.height;

        toolBar.frame = toolBarFrame;

        

        CGRect tableViewFrame = tableView.frame;

        tableViewFrame.size.height = toolBarFrame.origin.y;

        tableView.frame = tableViewFrame;

    }];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值