我以编程方式创建了UITextField
,(我不使用故事板)并使用以下代码将其添加为subview
到ViewController
:
@interface ViewController ()
@property (nonatomic, strong) UITextField *searchLocationBar;
@end
...
@synthesize searchLocationBar;
...
self.searchLocationBar = [[UITextField alloc] init];
self.searchLocationBar.frame = CGRectMake(0.0f, 0.0f, 320.0f, 40.0f);
self.searchLocationBar.delegate = self;
self.searchLocationBar.borderStyle = UITextBorderStyleRoundedRect;
self.searchLocationBar.placeholder = @"a temporary placeholder";
self.searchLocationBar.userInteractionEnabled = YES;
self.searchLocationBar.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:self.searchLocationBar];
然而,当我点击textfield
时,我无法输入任何文字 - 没有任何反应。它没有被任何其他视图重叠。
我检查了UITextfield not editable-iphone但没有效果
我是新手,完全确定我只是错过了一些东西 - 请指教。 谢谢!
修改 还有一件事:我有一个分配给self.view的Google Maps GMSMapView self.view = mapView_;如Google API文档中所述。 经过一些测试后,我发现通过此声明,所有控件都可以完美地工作,但不是文本字段。我不希望将地图视图移动到任何子视图,因为我需要重写很多东西。
有人可以添加任何建议吗?
答案 0 :(得分:1)
在Xcode 5中,您的代码应该可以正常工作。请检查您的Xcode版本。您的代码可能与Xcode版本有关。您可以尝试以下方式。
UITextField *lastName = [[[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)];
[self.view addSubview:lastName];
lastName.placeholder = @"Enter your last name here"; //for place holder
lastName.textAlignment = UITextAlignmentLeft; //for text Alignment
lastName.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:14.0]; // text font
lastName.adjustsFontSizeToFitWidth = YES; //adjust the font size to fit width.
lastName.textColor = [UIColor greenColor]; //text color
lastName.keyboardType = UIKeyboardTypeAlphabet; //keyboard type of ur choice
lastName.returnKeyType = UIReturnKeyDone; //returnKey type for keyboard
lastName.clearButtonMode = UITextFieldViewModeWhileEditing;//for clear button on right side
lastName.delegate = self; //use this when ur using Delegate methods of UITextField
还有很多其他可用的属性,但是我们经常使用它们很少。如果您想了解更多属性以及如何使用它们,请参阅以下链接。 您还可以为UITextField创建属性。这两种方式都可以在Xcode中正常工作。
答案 1 :(得分:1)
你忘了添加:
[self.view bringSubviewToFront:self.searchLocationBar];