본문 바로가기

iPhone

custom keyboard

1) Overlay an image.
2) Overlay a button.

And you’re done. Here’s the code I used:

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

// locate keyboard view

UIWindow* tempWindow = [[[UIApplication sharedApplicationwindowsobjectAtIndex:1];

UIView* keyboard;

UIImageView *decimalKeyboard = [[UIImageView allocinitWithFrame:CGRectMake(00320216)];

decimalKeyboard.image = [UIImage imageNamed:@"decimal_keyboard.png"];

UIButton *decimalButton = [[UIButton allocinitWithFrame:CGRectMake(016510553)];

[decimalButton addTarget:self action:@selector(decimalButtonPressed:)forControlEvents:UIControlEventTouchUpInside];

for(int i = 0; i < [tempWindow.subviews count]; i++) {

keyboard = [tempWindow.subviews objectAtIndex:i];

// keyboard view found; add the custom button to it

if([[keyboard descriptionhasPrefix:@”<UIKeyboard”] == YES)

{

[keyboard addSubview:decimalKeyboard];

[keyboard addSubview:decimalButton];

}

}

[decimalKeyboard release];

[decimalButton release];

}


+ 추가
출처: http://cafe.naver.com/mcbugi/62349 

3.1.x에서는 windows 서브뷰에 항상 키보드 뷰가 있었지만 ...
4.0 부터는 windows 에 항상 있는게 아니고, 키보드를 불렀을때 -_- 붙어서 오더라고요.. 그래서

Done 버튼 붙이는 예제 입니다. (Number 패드에)

아래 액션을 Text필드의 BeginTouched 에 연결 시킵니다.
 // 키보드가 나왔을때랑 사라질때의 이벤트를 잡아냅니다.
//3.1.X 에서는 UIKeyboardWillShowNotification 으로 잡지만
// 4.0 때문에 --; DidShow로 잡아줬습니다.
//그래야 윈도우에 키보드가 있더라고요 ;;;
-(IBAction)FieldTouched{
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    
    
}

//키보드가 나왔을때 Done 버튼 붙여주기 
- (void)keyboardWillShow:(NSNotification *)note {  
    
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown.png"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(backgroundTap:) forControlEvents:UIControlEventTouchUpInside];

    //3.1.x 와 4.0 호환 키보드 붙이기
    for( UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows] ){
        for( UIView *keyboard in [keyboardWindow subviews] ){
            NSString *desc = [keyboard description];
            if( [desc hasPrefix:@"<UIKeyboard"]==YES ||
               [desc hasPrefix:@"<UIPeripheralHostView"] == YES ||
               [desc hasPrefix:@"<UISnap"] == YES )
            {
                [keyboard addSubview:doneButton];
            }
        }
    }
    
}

// 키보드가 없어질때 Done 버튼을 삭제 합니다.
- (void)keyboardWillHide:(NSNotification *)note {  
    
    for( UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows] ){
        for( UIView *keyboard in [keyboardWindow subviews] ){
            NSString *desc = [keyboard description];
            if( [desc hasPrefix:@"<UIKeyboard"]==YES ||
               [desc hasPrefix:@"<UIPeripheralHostView"] == YES ||
               [desc hasPrefix:@"<UISnap"] == YES )
            {
                for(UIView *subview in [keyboard subviews])
                {
                    [subview removeFromSuperview];
                }
                
            }
        }
    }
}

 

'iPhone' 카테고리의 다른 글

Using The Document Directory To Store Files  (0) 2011.08.26
iphone 로컬라이징  (0) 2011.08.24
개발자료 정리  (0) 2011.08.24
NSString 자주 쓰는 함수  (0) 2011.08.22
UIImageView URL로 로드하기  (0) 2011.08.22