实际应用中经常用的代码片段,但是没有必要记住的

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 08:53   110   0

目录

1、获取键盘的高度

2、创建键盘上的完成按钮

3、设置button按钮中的文字居中方式

4、自己绘制一张背景图

1、获取键盘的高度

NSDictionary *userInfo = [aNotification userInfo];

NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

CGRect keyboardRect = [aValue CGRectValue];

self.keyboardEndHeight = keyboardRect.size.height;

self.keyboardEndHeight 自定义的


2、创建键盘上的完成按钮

- (UIToolbar *)createToolbar

{

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, KSCREENSIZE.width, 44)];

UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonSystemItemDone target:self action:@selector(textFieldDone)];

toolBar.items = @[space, done];

return toolBar;

}

- (void)textFieldDone

{

[[self findFirstResponder] resignFirstResponder];

}


3、设置button按钮中的文字居中方式

[btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

UIToolbar *toolBar = [self createToolbar];

textField.inputAccessoryView = toolBar;

4、自己绘制一张背景图

// 不同情况的背景图 可以设置高亮状态下的颜色

+ (UIImage *)backgroundImageForButtonWithBounds:(CGRect)bounds normalColor:(UIColor*)normalColor hightedColor:(UIColor*)hightedColor isShenqing:(BOOL)shenqing

{//自绘了一张背景图

CGRect canvasRect = bounds;

CGRect paintingRect = CGRectMake(0, 0, canvasRect.size.width, canvasRect.size.height);;

UIGraphicsBeginImageContext(canvasRect.size);

CGContextRef context = UIGraphicsGetCurrentContext();

if (shenqing) {

CGContextSetFillColorWithColor(context, [normalColor CGColor]);

} else {

CGContextSetFillColorWithColor(context, [hightedColor CGColor]);

}

CGContextFillRect(context, paintingRect);

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return image;

}




分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP