OC学习67:键盘上添加工具条

张建 lol

前言

在实现项目中,我们在使用 UITextField/UITextView 时,往往需要唤起键盘进行文本输入,而在某些时候,我们需要在键盘上方添加一个工具条

添加工具条的方法

  1. 自定义工具条视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UnlockToolV : UIView
// 忘记密码
@property (nonatomic,strong)UIButton * forgetBtn;
// 面容解锁
@property (nonatomic,strong)UIButton * faceUnlockBtn;
@end

NS_ASSUME_NONNULL_END

=================================================

#import "UnlockToolV.h"

@implementation UnlockToolV
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self initUI];
[self initMasonry];
}
return self;
}

// 初始化UI
- (void)initUI{
self.backgroundColor = kWhiteColor;

[self addSubview:self.forgetBtn];
[self addSubview:self.faceUnlockBtn];
}

// 初始化Masonry
- (void)initMasonry{
[self.forgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(20);
make.centerY.mas_equalTo(0);
}];

CGFloat faceUnlockBtn_w = self.faceUnlockBtn.width;
CGFloat faceUnlockBtn_h = self.faceUnlockBtn.height;
[self.faceUnlockBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-20);
make.centerY.mas_equalTo(0);
make.size.mas_equalTo(CGSizeMake(faceUnlockBtn_w, faceUnlockBtn_h));
}];
}

#pragma mark -懒加载
- (UIButton *)forgetBtn{
if (!_forgetBtn) {
_forgetBtn = [UIButton initBtnWithFrame:CGRectZero bgColor:nil hidden:NO];
[_forgetBtn setTitle:@"忘记密码" titleColor:kMainThemeColor font:12 isBlod:NO];
}
return _forgetBtn;
}
- (UIButton *)faceUnlockBtn{
if (!_faceUnlockBtn) {
_faceUnlockBtn = [UIButton initBtnWithFrame:CGRectMake(0, 0, 70, 20) bgColor:nil hidden:NO];
[_faceUnlockBtn setTitle:@"面容解锁" titleColor:kMainThemeColor font:12 isBlod:NO];
[_faceUnlockBtn setImg:@"unlock_face" selectedImg:nil isSelect:NO];
[_faceUnlockBtn setStyle:BtnStyle_ImgLeft space:5];
}
return _faceUnlockBtn;
}

@end
  1. 在需要设置的控件上设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 声明头文件
#import "UnlockToolV.h"

// 声明属性
@property (nonatomic,strong)UnlockToolV * toolV;

// 设置
[self.codeV.bottomTF setInputAccessoryView:self.toolV];

// 懒加载
- (UnlockToolV *)toolV{
if (!_toolV) {
_toolV = [[UnlockToolV alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
}
return _toolV;
}
  • Post title:OC学习67:键盘上添加工具条
  • Post author:张建
  • Create time:2023-07-04 13:45:43
  • Post link:https://redefine.ohevan.com/2023/07/04/OC/OC学习67:键盘上添加工具条/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.
On this page
OC学习67:键盘上添加工具条