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

来源:互联网 发布:sql 附加数据库 编辑:程序博客网 时间:2024/06/05 05:42

目录

1、获取键盘的高度

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

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

4、自己绘制一张背景图

1、获取键盘的高度

    NSDictionary *userInfo = [aNotificationuserInfo];

    NSValue *aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValueCGRectValue];

    self.keyboardEndHeight = keyboardRect.size.height;

   self.keyboardEndHeight   自定义的


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

- (UIToolbar *)createToolbar

{

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

    UIBarButtonItem *space = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];

    UIBarButtonItem *done = [[UIBarButtonItemalloc] initWithTitle:@"完成"style:UIBarButtonSystemItemDonetarget:selfaction:@selector(textFieldDone)];

    

    toolBar.items =@[space, done];

    return toolBar;

}

- (void)textFieldDone

{

    [[selffindFirstResponder] resignFirstResponder];

}


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

    [btn  setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];

    UIToolbar *toolBar = [selfcreateToolbar];

    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, [normalColorCGColor]);

    } else {

        CGContextSetFillColorWithColor(context, [hightedColorCGColor]);

    }

    CGContextFillRect(context, paintingRect);

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}




0 0
原创粉丝点击