点击按钮弹出键盘或者特定的键盘

2017-07-30  本文已影响76人  JaromeHuang

问题

1.gif

解决办法

@property (nullable, readwrite, strong) UIView *inputView;    // 设置键盘内容         
@property (nullable, readwrite, strong) UIView *inputAccessoryView;// 设置键盘的附属内容

但UIButton按钮虽然是有这个两个属性,但可惜是可读的属性,所以就没法直接用。但如果我们自定义UIButton ,在UIButton添加一个UITextField的属性就可以用了

自定义按钮

#import <UIKit/UIKit.h>

@interface CustomBtn : UIButton
@property (nonatomic, weak) UITextField *texField;

@end
- (void)awakeFromNib
{
    [super awakeFromNib];
    
    UITextField *texField = [[UITextField alloc] init];
    texField.hidden = YES;
    [self addSubview:texField];
    self.texField = texField;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UITextField *texField = [[UITextField alloc] init];
        texField.hidden = YES;
        [self addSubview:texField];
        self.texField = texField;
    }
    return self;
}
@end

创建自定义的按钮,并使用

@interface ViewController ()<KeyboardToolDelegate>
@property (weak, nonatomic) IBOutlet CustomBtn *timeBtn;
@property (nonatomic, strong) UIDatePicker *datePicker;
@end
    self.timeBtn.texField.inputAccessoryView = keyboardTool;
    self.timeBtn.texField.inputView = self.datePicker;

这样就能达到我们要的效果

上一篇 下一篇

猜你喜欢

热点阅读