我对 iOS 7.1.2 上的 UITextField 使用 iPhone4S 进行了一些练习。
界面:
第一个版本的界面很简单,只是有一个自定义的UITextField
命名MyUITextField
和一个UIButtom
用于取消搜索操作的对象。在 textField 内部,它的leftView
属性最初设置为一个UIButton
对象,该对象的背景图像是一个放大镜。当用户在 textField 中点击时,该放大镜按钮将被移除,并且 leftView 属性也将设置为一个新的 UIButton 对象,该对象的背景图像是一个倒三角形。当用户点击三角形按钮时,应用程序将创建一个新的视图对象。
主要代码:
//In this version,the textField's original frame property is set to (10,40,250,30)
//and the main operations are like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextField.delegate = self;
self.myTextField.leftViewMode = UITextFieldViewModeUnlessEditing;
self.myTextField.leftView = [self creCustomSearchBar];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyBoardWillAppear:)
name:@"UIKeyboardWillShowNotification"
object:nil];
[self.cancleSearchButton addTarget:self
action:@selector(cancleSearch:)
forControlEvents:UIControlEventTouchUpInside];
}
/*when tapping in the textField,keyboard will appear*/
- (void)keyBoardWillAppear:(NSNotification *)notification
{
CGRect newLeftViewIndicatorRect = CGRectMake(20,5,20,20);
UIButton *newLeftViewIndicator = [[UIButton alloc] initWithFrame:newLeftViewIndicatorRect];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"searchbar_textfield_down_icon@2x" ofType:@"png"];
[newLeftViewIndicator setBackgroundImage:[UIImage imageWithContentsOfFile:imagePath] forState:UIControlStateNormal];
[newLeftViewIndicator addTarget:self action:@selector(createActuralLeftView:) forControlEvents:UIControlEventTouchUpInside];
self.myTextField.leftView = newLeftViewIndicator;
/*in the second verson need to call */
//[self adjustViewFrame];
[self.cancleSearchButton setTitle:@"取消" forState:UIControlStateNormal];
}
- (void)createActuralLeftView:(UIButton *)leftViewButton
{
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(tapRegionAboveKeyboard:)];
[self.view addGestureRecognizer:tapGesture];
NewLeftView *leftView = [[NewLeftView alloc] initWithFrame:CGRectMake(10, 80, 140, 100)];
leftView.delegate = self;
[self.view addSubview:leftView];
}
- (void)tapRegionAboveKeyboard:(UITapGestureRecognizer *)tapGesture
{
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[NewLeftView class]]) {
[v removeFromSuperview];
[self.view removeGestureRecognizer:tapGesture];
return;
}
}
}
//in the second version need to call
- (void)adjustViewFrame
{
[[self.myTextField class] animateWithDuration:0.05 animations:^{
[self.myTextField setFrame:CGRectMake(self.myTextField.frame.origin.x, 40, self.myTextField.frame.size.width, self.myTextField.frame.size.height)];
}];
[[self.cancleSearchButton class] animateWithDuration:0.05 animations:^{
[self.cancleSearchButton setFrame:CGRectMake(self.cancleSearchButton.frame.origin.x, 40, self.cancleSearchButton.frame.size.width, self.cancleSearchButton.bounds.size.height)];
}];
}
在第一个版本中,一切正常。
但是在第二个版本中,我将textField frame 属性设置为(10,260,250,30),所以我需要调用中的adjustViewFrame
方法keyBoardWillAppear:
来重新定位textField,以防被键盘遮挡。
问题来了:textField的位置正确移动到正确的位置,但是当我点击倒三角形按钮时,textField消失了。
我无法弄清楚这里出了什么问题。