在我的项目中,我使用的是UITextField,它嵌入带有圆角的纯白色UIButton中,以创建“漂亮”的文本字段。此代码在不同视图中多次使用,因此我决定为此目的创建自定义UIView。这个自定义UIView的代码:
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 :(得分:4)
好的,我有解决方案。 raaz对UIResponder课程的观点让我觉得在响应者链中肯定存在错误,因此我做了一些研究并找到了这个主题: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
由于可点击区域填写了整个BSCustomTextField视图,我们只需在pointInside中返回YES:withEvent:
谢谢大家指出我正确的方向。
答案 1 :(得分:1)
此类具有实例方法becomeFirstResponder:
&amp; isFirstResponder:
另外,不要忘记将textfield editing
属性设置为YES
答案 2 :(得分:0)
哦,你这样做是完全错的。您应该将可伸缩图像添加到UIImageView中并将其放在UITextField下面。
UIImage *backgroundImage = [[UIImage imageNamed:@"fieldBackground.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
或者您可以尝试在UIButton中禁用userInteraction。