博客> 输入框TextField 相关
输入框TextField 相关
2018-12-12 15:41 评论:0 阅读:833 OS_JJ
ios 点击textfield上移视图

第一、点击textfield上移视图(键盘上部按钮或者标签)

一.通知中心

    1.初始化通知中心,设置控制器为监听器
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(KeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    2.监听器消失时,移除通知中心
        - (void)dealloc{
               [[NSNotificationCenter defaultCenter] removeObserver:self];

        }
    3.实现监听器的方法
        - (void)KeyboardWillChangeFrame:(NSNotification *)note{ 
                NSLog(@"%@",note.userInfo);

                CGRect keyboard = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
                CGFloat trans = keyboard.origin.y - HEIGHT;
                self.view.transform = CGAffineTransformMakeTranslation(0, trans);

        }

    4.拖动或点击textfield上部视图时,键盘消失
            - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
                [self.view endEditing:YES];         
            }
         或者是
            - (void)touchesBegan:(NSSet<UITouch> *)touches withEvent:(UIEvent *)event{
                [self.view endEditing:YES];
            }

二.textfield的代理方法

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{

    CGRect frame = textView.frame;

    int offset = frame.origin.y + 80 - (HEIGHT - 216.0);//iPhone键盘高度216,iPad的为352

    [UIView animateWithDuration:0.5 animations:^{

        //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

        if(offset > 0)

            self.view.frame = CGRectMake(0, -offset, WIDTH, HEIGHT);
            [self.navigationController setNavigationBarHidden:NO animated:YES];

    }];

    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    }];

    return YES;
}

三、工具栏

     //创建工具条
     UIToolbar *toolbar=[[UIToolbar alloc]init];
     //设置工具条的颜色
     toolbar.barTintColor=[UIColor brownColor];
     //设置工具条的frame
     toolbar.frame=CGRectMake(0, 0, 320, 44);

     //给工具条添加按钮
        UIBarButtonItem *item0=[[UIBarButtonItem alloc]initWithTitle:@"上一个" style:UIBarButtonItemStylePlain target:self action:@selector(click) ];

         UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"下一个" style:UIBarButtonItemStylePlain target:self action:@selector(click)];

         UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
         UIBarButtonItem *item3=[[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(click)];

      toolbar.items = @[item0, item1, item2, item3];
     //设置文本输入框键盘的辅助视图
     self.textfield.inputAccessoryView=toolbar;

四.第三方库 IQKeyboardManager

超级传送门

第二、监听输入框删除键

    1.新建类文件,集成系统 UItextField
    2.添加删除键代理 A
    3.重写 deleteBackward 方法
    4.在需要用的类文件中,添加代理,实现 删除键代理A

    举例:
    1、MSUTextField.h 文件:

        #import <UIKit>

        @protocol MSUTextFieldDelegate <NSObject>

        - (void)deleteBtnClick:(UITextField *)textField;

        @end

        @interface MSUTextField : UITextField

        @property (nonatomic , weak) id<MSUTextFieldDelegate> delegate;

        @end

    2、MSUTextField.m 文件:重写 deleteBackward 方法

        #import "MSUTextField.h"

        @implementation MSUTextField

        - (void)deleteBackward{
            [super deleteBackward];

            if (self.delegate && [self.delegate respondsToSelector:@selector(deleteBtnClick:)]) {
                [self.delegate deleteBtnClick:self];
            }

        }

        @end

    3、要用的类文件 :MSUAgainSetPWDController.m 中

        1)遵循代理协议 <MSUTextFieldDelegate>
        2) 实现代理方法 deleteBtnClick
            - (void)deleteBtnClick:(UITextField *)textField{
                if (textField.text.length > 0 || textField.text.length == 0) {
                    UILabel *label = self.btnArr[textField.text.length];
                    label.text = @"";
                    self.pwd2Str = textField.text;
                }
            }

第三、更改输入框占位符字体大小、颜色等

        self.shopTF = [[UITextField alloc] init];
        _shopTF.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"商品名称" attributes:@{NSForegroundColorAttributeName: HEXCOLOR(0x757575)}];
        _shopTF.frame = CGRectMake(14, 0, WIDTH-28, 40);
        _shopTF.font = [UIFont systemFontOfSize:14];
        _shopTF.textColor = HEXCOLOR(0x333333);
        //        _shopTF = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];
        //        _shopTF = UITextFieldViewModeAlways;
        [topView addSubview:_shopTF];
收藏
0
sina weixin mail 回到顶部