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
  • 相关阅读:
    Maven 环境的配置
    zTree的简单例子
    plsql免安装客户端的配置
    HDU 1232 畅通工程
    HDU 5698 瞬间移动
    Codeforces 1015E1 Stars Drawing (Easy Edition)
    Codeforces 784B Santa Claus and Keyboard Check
    Codeforces 500C New Year Book Reading
    NSarray 赋值 拷贝 等问题记录
    UINavigationController 操作记录
  • 原文地址:https://www.cnblogs.com/pengsi/p/7211728.html
Copyright © 2011-2022 走看看