本人也是看了一下 这个用view手写工具栏也是比较好用 适合初学者 纯代码简单

小编我就是看不惯网上的代码什么都是用的SB(拖拽),你那么厉害怎么不拖一个QQ出来呢。
絮叨完毕 干活
下面是完整代码
SDAutoLayout.h 是第三方常用屏幕适配
#import "toolController.h"
#import <SDAutoLayout.h>
@interface toolController ()
{
// 呼出键盘输入框
UITextField * textfield;
// 键盘内输入框
UITextField * textfield2;
UIView * suspendView;
}
@end
@implementation toolController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化输入框
textfield = [[UITextField alloc]init];
textfield.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textfield];
textfield.sd_layout
.topSpaceToView(self.view, 100)
.leftSpaceToView(self.view , 20)
.widthIs(200)
.heightIs(40);
UITapGestureRecognizer *tapgest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackAction)];
[self.view addGestureRecognizer:tapgest];
}
//点击屏幕回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)viewWillAppear:(BOOL)animated {
// 添加对键盘的监控
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)tapBackAction {
[self.view endEditing:YES];
}
- (void)keyBoardWillShow:(NSNotification *) note {
// 获取用户信息
NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
// 获取键盘高度
CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyBoardHeight = keyBoardBounds.size.height;
// 获取键盘动画时间
CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 定义好动作
void (^animation)(void) = ^void(void) {
suspendView.transform = CGAffineTransformMakeTranslation(0, - keyBoardHeight);
};
if (animationTime > 0) {
[UIView animateWithDuration:animationTime animations:animation];
} else {
animation();
}
}
- (void)keyBoardWillHide:(NSNotification *) note {
// 获取用户信息
NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
// 获取键盘动画时间
CGFloat animationTime = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// 定义好动作
void (^animation)(void) = ^void(void) {
suspendView.transform = CGAffineTransformIdentity;
};
if (animationTime > 0) {
[UIView animateWithDuration:animationTime animations:animation];
} else {
animation();
}
}
- (UIView *)inputAccessoryView
{
// 我在这里用的是SDAutoLayout屏幕适配方式,如果初学者不太熟悉的话可以用系统的方式来定义位置、
// 例如:
// UIButton * btn =[[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 50)];
// [btn setTitle:@"例子" forState:UIControlStateNormal];
// 之后直接添加到键盘的view上就可以了
// [containerView addSubview:btn];
// 创建的发送按钮 在inputAccessoryView方法中创建
UIButton *button = [[UIButton alloc]init];
[button setTitle:@"发送" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// 自定义表情按钮
UIButton * BQiamge = [[UIButton alloc]init];
[BQiamge setImage:[UIImage imageNamed:@"表情"] forState:UIControlStateNormal];
[BQiamge addTarget:self action:@selector(BQbtn:) forControlEvents:UIControlEventTouchUpInside];
// 键盘上方的view
UIView *containerView = [UIView new];
containerView.frame = CGRectMake(0, 0, 10, 45);
containerView.backgroundColor = [UIColor whiteColor];
[containerView addSubview:button];
// 用SDAutoLayout来实现屏幕适配
button.sd_layout
.centerYEqualToView(containerView)
.rightSpaceToView(containerView, 5)
.widthIs(40)
.heightIs(40);
// 键盘上方的输入框
textfield2 = [[UITextField alloc]init];
textfield2.borderStyle = UITextBorderStyleRoundedRect;
// 输入框设置圆角
textfield2.layer.cornerRadius = 15;
//将多余的部分切掉
textfield2.layer.masksToBounds = YES;
textfield2.placeholder = @"请输入....";
textfield2.backgroundColor = [UIColor grayColor];
[containerView addSubview:textfield2];
// 键盘上方输入框适配
textfield2.sd_layout
.centerYEqualToView(containerView)
.leftSpaceToView(self, 5)
.widthIs(270)
.heightIs(40);
[containerView addSubview:BQiamge];
// 表情按钮适配
BQiamge.sd_layout
.centerYEqualToView(containerView)
.leftSpaceToView(textfield2, 15)
.widthIs(30)
.heightIs(30);
return containerView;
}
网友评论