给iphone的键盘加上UITextField

原文地址:给键盘加上UITextField
[url]http://www.aisidechina.com/forum/viewthread.php?tid=914[/url]
效果:
[img]http://www.aisidechina.com/forum/attachments/month_1001/100111144100673c49230058bd.png[/img]

.h文件:[code]#import <UIKit/UIKit.h>


@interface ScoreBar : UIToolbar {
UITextField *nameTextField;
UILabel *scoreLabel;
}
@property (nonatomic, retain) UITextField *nameTextField; //toolbar上的输入框
@property (nonatomic, retain) UILabel *scoreLabel; //toolbar上的按钮

-(void)keyboareWillShow:(NSNotification*)aNotification; //键盘将要出现时调用

@end[/code].m文件[code]#import "ScoreBar.h"


@implementation ScoreBar
@synthesize nameTextField;
@synthesize scoreLabel;

- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
self.hidden = YES;
self.tintColor = [UIColor grayColor];
/*
初始化toolbar上的textField和button
*/
......略
/*
设定UIKeyboardWillShowNotification调用的方法
*/
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboareWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
return self;
}


- (void)drawRect:(CGRect)rect {
[super drawRect:rect];//调用父类的方法,这个一定要写
// Drawing code
}


- (void)dealloc {
.......
}

-(void)keyboareWillShow:(NSNotification*)aNotification{
/* 取得键盘的frame */
NSDictionary *info = [aNotification userInfo];
NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect frame;
[aValue getValue:&frame];
//设定toolbar的frame
self.frame = CGRectMake(frame.origin.x, frame.origin.y-44, frame.size.width, 44);

UIWindow *tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
UIView *keyboard;
/*遍历windwos中所有的view,取得键盘view*/
for(int i = 0;i<tempWindow.subviews.count;i++){
keyboard = [tempWindow.subviews objectAtIndex:i];

if([[NSString stringWithUTF8String:object_getClassName(keyboard)] isEqualToString:@"UIKeyboard"]){
self.hidden = NO;
[keyboard addSubview:self];
[keyboard setBackgroundColor:[UIColor blackColor]];
}
}
}
@end[/code]初始化toolbar:[code]ScoreBar *scoreBar = [[ScoreBar alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
scoreBar.nameTextField.delegate = self;[/code]使用becomeFirstResponder调出键盘:
[code][scoreToolBar.nameTextField becomeFirstResponder];[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值