zoukankan      html  css  js  c++  java
  • iOS--inputView和inputAccessoryView

    iOS–inputView和inputAccessoryView


    什么是inputView和inputAccessoryView?

    如果是UITextField和UITextView,下面是声明文件源代码:

    // set while first responder, will not take effect until reloadInputViews is called.
    @property (readwrite, retain) UIView *inputView;             
    @property (readwrite, retain) UIView *inputAccessoryView;

    如果是其他的控件,继承了UIResponder,(UIView 是UIResponder的子类),下面是声明文件源代码:

    // Called and presented when object becomes first responder.  Goes up the responder chain.
    @property (nonatomic, readonly, retain) UIView *inputView NS_AVAILABLE_IOS(3_2);
    @property (nonatomic, readonly, retain) UIView *inputAccessoryView NS_AVAILABLE_IOS(3_2);

    自定义TextField或TextView

    自定义只需要重写下面的两个属性

    textField.inputView = [UIView alloc] init];
    textField.inputAccessoryView = [UIToolbar alloc] init];

    自定义继承了UIResponder类的控件

    需要自定义UIResponder的子类,我们需要在重新定义一个子类,然后再这个子类中声明inputViewinputAccessoryView为可读可写属性。然后重写getter方法。同时还需要覆盖-(BOOL)canBecomeFirstResponder方法。

    下面以UIButton为例子的子类重设代码:

    #import <UIKit/UIKit.h>
    
    @interface ABEButton : UIButton
    
    @property (nonatomic, strong)UIToolbar *inputAccessoryView;
    @property (nonatomic, strong)UIView *inputView;
    
    @end
    #import "ABEButton.h"
    
    #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
    
    @implementation ABEButton
    
    -(BOOL)canBecomeFirstResponder
    {
        return YES;
    }
    
    #pragma mark- Getter, Setter
    - (UIView*)inputView{
        if (!_inputView) {
            _inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 220)];
            _inputView.backgroundColor = [UIColor redColor];
        }
        return _inputView;
    }
    
    - (UIToolbar*)inputAccessoryView{
        if (!_inputAccessoryView) {
            _inputAccessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
            _inputAccessoryView.backgroundColor = [UIColor grayColor];
            UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"隐藏" style:UIBarButtonItemStylePlain target:self action:@selector(clickFinishButton:)];
            _inputAccessoryView.items = @[item];
        }
        return _inputAccessoryView;
    }
    
    #pragma mark- Private method
    - (void)clickFinishButton:(ABEButton*)buttonItem{
        [self resignFirstResponder];
    }
    @end
    

    这样,我们就可以使用点击button,弹出inputView了。


    注意:不要忘记为button添加becomeFirstResponder。

  • 相关阅读:
    【2020-05-17】人生十三信条
    【2020-05-16】评价与骄傲
    【2020-05-15】每天都充满向上的激情
    【04NOIP普及组】火星人(信息学奥赛一本通 1929)(洛谷 1088)
    next_permutation(全排列算法)
    【03NOIP普及组】麦森数(信息学奥赛一本通 1925)(洛谷 1045)
    快速幂
    【03NOIP普及组】栈(信息学奥赛一本通 1924)(洛谷 1044)
    【06NOIP普及组】数列(信息学奥赛一本通 1937)(洛谷 1062)
    【00NOIP普及组】计算器的改良(信息学奥赛一本通 1910)(洛谷 1022)
  • 原文地址:https://www.cnblogs.com/AbeDay/p/5026895.html
Copyright © 2011-2022 走看看