zoukankan      html  css  js  c++  java
  • ToolBar跟随键盘弹出和隐藏

    实现效果如下:

    代码实现如下:

    //
    //  ViewController.m
    //  PopToolBarDemo
    //
    //  Created by 思 彭 on 2017/7/20.
    //  Copyright © 2017年 思 彭. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()<UITextFieldDelegate>
    
    @property (weak, nonatomic) IBOutlet UITextField *myTextField;
    @property (nonatomic, strong) UIToolbar *toolBarView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.myTextField.delegate = self;
        
        // 监听键盘的弹出
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
        
        CGSize size = self.view.frame.size;
        // 创建一个工具条,并设置它的大小和位置
        UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, size.width, 46)];
        self.toolBarView = toolbar;
        // 设置工具条的style
        [toolbar setBarStyle:UIBarStyleDefault];
        
        // 创建使用文本标题的UIBarButtonItem
    //    UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(onClick:)];
        
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(onClick:)];
        [spaceItem setTag:1];
        
        // 创建使用自定义图片的UIBarButtonItem
        UIBarButtonItem* centerItem = [[UIBarButtonItem alloc]
                                       initWithImage:[[UIImage imageNamed:@"exam_stopwatch_icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]
                                       style:UIBarButtonItemStylePlain
                                       target:self
                                       action:@selector(onClick:)];
        [centerItem setTag:2];
        // 创建使用系统图标的UIBarButtonItem
        UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onClick:)];
        [rightItem setTag:3];
        // 为工具条设置工具按钮  注意设置间距
        NSArray* barButtonItems = [NSArray arrayWithObjects:spaceItem, centerItem, spaceItem, rightItem,spaceItem,  nil];
        
        [toolbar setItems:barButtonItems animated:YES];
        // 将工具条添加到当前应用的界面中
        [self.view addSubview:toolbar];
    }
    
    - (void)onClick: (UIBarButtonItem *)item {
        NSInteger tag = [item tag];
        switch (tag) {
            case 2:
                NSLog(@"第一个");
                break;
            case 3:
                NSLog(@"第二个");
                break;
            default:
                break;
        }
    }
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        NSLog(@"textField.text = %@",textField.text);
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
        [self.myTextField resignFirstResponder];
        return YES;
    }
    
    - (void)keyBoardWillChangeFrame:(NSNotification*)notification{
        
        // 键盘显示隐藏完毕的frame
        CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        
        // 动画时间
        CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
        
        if (frame.origin.y == [UIScreen mainScreen].bounds.size.height) {
            // 没有弹出键盘
            [UIView animateWithDuration:duration animations:^{
                
                self.toolBarView.transform = CGAffineTransformIdentity;
                
            }];
        } else{ // 弹出键盘
            
            // 工具条往上移动258
            [UIView animateWithDuration:duration animations:^{
                
                self.toolBarView.transform = CGAffineTransformMakeTranslation(0, -frame.size.height-44);
            }];
        }
    }
    
    @end
  • 相关阅读:
    20175122邱昕 网络对抗技术exp2后门的原理与实践
    2019-2020 20175122邱昕《网络对抗技术》Exp1 PC平台逆向破解
    day04 python——进程和线程
    day 03 python——面向对象编程进阶
    day02 python——面向对象进阶
    day01 python——面向对象编程基础
    day05
    day04 五层模型之间的通信过程及Linux的目录相关内容
    day03 计算机的性能及系统启动
    Linux入门终端命令
  • 原文地址:https://www.cnblogs.com/pengsi/p/7211728.html
Copyright © 2011-2022 走看看