2010-12-29 2 views
3

내 프로젝트에서 "아름다운"텍스트 필드를 만들기 위해 둥근 모서리가있는 흰색 UIButton에 일반 UIButton에 포함 된 UITextField를 사용하고 있습니다. 이 코드는 다른 뷰에서 여러 번 사용되므로이 용도로 사용자 정의 UIView를 작성하기로 결정했습니다. 이 사용자 정의의 UIView의 코드 :UIView에 포함 된 UITextField가 터치 이벤트에 응답하지 않습니다

BSCustomTextField.h

#import <Foundation/Foundation.h> 

@interface BSCustomTextField : UIView { 
    UIButton* btnTextFieldContainer; 
    UITextField* textField; 
    NSString* text; 
} 

@property (retain, nonatomic) UIButton* btnTextFieldContainer; 
@property (retain, nonatomic) UITextField* textField; 
@property (retain, nonatomic) NSString* text; 

-(id)initWithPosition:(CGPoint)position; 
@end 

BSCustomTextField.m 그래서

#import "BSCustomTextField.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation BSCustomTextField 
@synthesize btnTextFieldContainer, textField, text; 

-(id)initWithPosition:(CGPoint)position{ 
    if (self == [super init]) { 
     btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)]; 
     btnTextFieldContainer.backgroundColor = [UIColor whiteColor]; 
     [[btnTextFieldContainer layer] setCornerRadius:3.]; 
     [[btnTextFieldContainer layer] setMasksToBounds:YES]; 
     [[btnTextFieldContainer layer] setBorderWidth:0.]; 

     textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)]; 

     textField.backgroundColor = [UIColor whiteColor]; 
     textField.clearButtonMode = UITextFieldViewModeAlways; 
     textField.returnKeyType = UIReturnKeySend; 
     textField.keyboardType = UIKeyboardTypeEmailAddress; 
     textField.font    = [UIFont fontWithName:@"Helvetica-Bold" size:20.]; 

     [btnTextFieldContainer addSubview:textField]; 
     [self addSubview:btnTextFieldContainer]; 
    } 
    return self; 
} 

-(void)dealloc{ 
    [btnTextFieldContainer release]; 
    [textField release]; 
    [super dealloc]; 
} 
@end 

, 아래의 코드를 참조 (컨테이너 뷰의 있는 viewDidLoad이보기를 사용하는 경우 뷰는 원하는 위치에 올바르게 렌더링되고 정확하게 지정된 것처럼 보이지만 터치 이벤트에는 반응하지 않으므로 터치해도 첫 번째 응답자가되지 않습니다.

코드 :

searchTextField = [[BSCustomTextField alloc] initWithPosition:CGPointMake(30, 150)]; 
searchTextField.textField.placeholder  = NSLocalizedString(@"lsUserName", @""); 
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:searchTextField]; 

[searchTextField.textField becomeFirstResponder] 프로그램이 제대로 작동 나타날 때까지 키보드를 만드는 호출.

하지만, 난 그냥 같은 내 컨테이너에 BSCustomTextField 인라인 코드를 포함하면 흥미로운 부분이있다 : 예상과 텍스트 필드가 터치에 반응로

UIButton* btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(30, 150, 260, 50)]; 
btnTextFieldContainer.backgroundColor = [UIColor whiteColor]; 
[[btnTextFieldContainer layer] setCornerRadius:3.]; 
[[btnTextFieldContainer layer] setMasksToBounds:YES]; 
[[btnTextFieldContainer layer] setBorderWidth:0.]; 

searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)]; 

searchTextField.backgroundColor = [UIColor whiteColor]; 
searchTextField.clearButtonMode = UITextFieldViewModeAlways; 
searchTextField.returnKeyType = UIReturnKeySend; 
searchTextField.keyboardType = UIKeyboardTypeEmailAddress; 
searchTextField.font   = [UIFont fontWithName:@"Helvetica-Bold" size:20.]; 
searchTextField.placeholder  = NSLocalizedString(@"lsUserName", @""); 

[btnTextFieldContainer addSubview:searchTextField]; 
[[(BSPlainHeaderWithBackButtonView*)self.view contentView] addSubview:btnTextFieldContainer]; 
[btnTextFieldContainer release]; 

모든 작품. searchTextField의 유형은 각 경우에 대해 헤더 파일에 올바르게 설정됩니다. 유일한 차이점은 첫 번째 경우 UIButton 및 UITextFiled에 추가로 UIView를 래핑한다는 것입니다. 텍스트 필드를 첫 번째 응답자가되도록하려면 어떻게해야할지 모르겠습니다. 사전에

고마워, 로마

+0

[[btnTextFieldContainer 레이어] setBorderWidth : 0.]; float 값이 포함되어 있지 않습니다. – raaz

+0

죄송합니다. 정확히 무슨 뜻인지 이해하지 못합니다. "0" float 값이고 예상 한 효과가 정확히 있습니다. UIButton 레이어의 경계를 0으로 설정합니다. – Romanticus

답변

0

아를 설정하는 것을 잊지 마세요 인스턴스 메소드에게 또한 becomeFirstResponder: & isFirstResponder:

있다 , 당신은 완전히 이런 식으로하는 것이 잘못되었습니다. UIImageView에 신축성있는 이미지를 추가하고 UITextField 아래에 배치해야합니다.

UIImage *backgroundImage = [[UIImage imageNamed:@"fieldBackground.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0]; 

또는 UIButton에서 userInteraction을 비활성화 할 수 있습니다.

+0

둥근 모서리 배경으로 "더미"버튼을 사용하는 것이 내가 원하는 결과를 얻는 가장 좋은 방법이 아니라는 것을 알고 있습니다.하지만 컨테이너 뷰에서 동일한 코드 인라인을 사용하면 모든 것이 예상대로 작동합니다. Btw. UIButton에서 사용자 상호 작용을 사용 중지해도 아무런 효과가 없습니다. 텍스트 필드는 여전히 응답하지 않습니다. – Romanticus

4

좋아, 해결책을 찾았습니다. UIResponder 클래스에 대한 raaz의 관점은 응답 체인에서 뭔가 잘못되었다고 생각하게 만들었습니다. 따라서 약간의 연구를 수행하여 정확히 같은 문제가 논의 된 Allowing interaction with a UIView under another UIView이라는 주제를 발견했습니다.
다음은 BSCustomTextField의 새로운 작업 코드입니다.m

#import "BSCustomTextField.h" 
#import <QuartzCore/QuartzCore.h> 


@implementation BSCustomTextField 
@synthesize btnTextFieldContainer, textField, text; 

-(id)initWithPosition:(CGPoint)position{ 
    if (self == [super init]) { 
    btnTextFieldContainer = [[UIButton alloc] initWithFrame:CGRectMake(position.x, position.y, 260, 50)]; 
    btnTextFieldContainer.backgroundColor = [UIColor whiteColor]; 
    [[btnTextFieldContainer layer] setCornerRadius:3.]; 
    [[btnTextFieldContainer layer] setMasksToBounds:YES]; 
    [[btnTextFieldContainer layer] setBorderWidth:0.]; 

    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 240, 30)]; 

    textField.backgroundColor = [UIColor whiteColor]; 
    textField.clearButtonMode = UITextFieldViewModeAlways; 
    textField.returnKeyType = UIReturnKeySend; 
    textField.keyboardType = UIKeyboardTypeEmailAddress; 
    textField.font    = [UIFont fontWithName:@"Helvetica-Bold" size:20.]; 

    [btnTextFieldContainer addSubview:textField]; 
    [self addSubview:btnTextFieldContainer]; 
    } 
    return self; 
} 

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { 
    // UIView will be "transparent" for touch events if we return NO 
    return YES; 
} 

-(void)dealloc{ 
    [btnTextFieldContainer release]; 
    [textField release]; 
    [super dealloc]; 
} 
@end 

클릭 가능한 영역은 우리가 단순히 pointInside에 YES를 반환 할 수 있습니다 전체 BSCustomTextField보기 채우고 이후 : withEvent :

올바른 방향으로 나를 가리키는 주셔서 모두 감사합니다.