diff --git a/EVNCustomSearchBar/EVNCustomSearchBar.m b/EVNCustomSearchBar/EVNCustomSearchBar.m index 46a1f92..7105fda 100644 --- a/EVNCustomSearchBar/EVNCustomSearchBar.m +++ b/EVNCustomSearchBar/EVNCustomSearchBar.m @@ -55,11 +55,14 @@ - (CGSize)intrinsicContentSize - (void)initView { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 44); - - [self addSubview:self.cancelButton]; + if (!_isHiddenCancelButton) + { + [self addSubview:self.cancelButton]; + self.cancelButton.hidden = YES; + } [self addSubview:self.textField]; - self.cancelButton.hidden = YES; + // self.backgroundColor = [UIColor colorWithRed:0.733 green:0.732 blue:0.756 alpha:1.000]; [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; @@ -122,21 +125,15 @@ - (void)setIconAlign:(EVNCustomSearchBarIconAlign)iconAlign - (void)ajustIconWith:(EVNCustomSearchBarIconAlign)iconAlign { - if (_iconAlign == EVNCustomSearchBarIconAlignCenter && ([self.text isKindOfClass:[NSNull class]] || !self.text || [self.text isEqualToString:@""] || self.text.length == 0)) + if (_iconAlign == EVNCustomSearchBarIconAlignCenter && ([self.text isKindOfClass:[NSNull class]] || !self.text || [self.text isEqualToString:@""] || self.text.length == 0) && ![_textField isFirstResponder]) { _iconCenterImgV.hidden = NO; _textField.frame = CGRectMake(7, 7, self.frame.size.width - 7*2, 30); _textField.textAlignment = NSTextAlignmentCenter; CGSize titleSize; // 输入的内容或者placeholder数据 - // if ([self.text isKindOfClass:[NSNull class]] || !self.text || [self.text isEqualToString:@""] || self.text.length == 0) - // { - // titleSize = [self.text sizeWithAttributes: @{NSFontAttributeName:_textField.font}]; - // } - // else - // { + titleSize = [self.placeholder?:@"" sizeWithAttributes: @{NSFontAttributeName:_textField.font}]; - // } NSLog(@"----%f", _textField.frame.size.width); CGFloat x = _textField.frame.size.width/2.f - titleSize.width/2.f - 30; @@ -230,15 +227,11 @@ - (void)setInputView:(UIView *)inputView _inputView = inputView; _textField.inputView = _inputView; } -//- (BOOL)isUserInteractionEnabled -//{ -// return YES; -//} -// -//- (BOOL)canBecomeFirstResponder -//{ -// return YES; -//} + +- (BOOL)isUserInteractionEnabled +{ + return YES; +} - (void)setInputAccessoryView:(UIView *)inputAccessoryView { @@ -401,7 +394,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N { if ([object isEqual:self] && [keyPath isEqualToString:@"frame"]) { - // _textField.frame = CGRectMake(7, 7, self.frame.size.width - 7*2, 30); + // _textField.frame = CGRectMake(7, 7, self.frame.size.width - 7*2, 30); NSLog(@"----%f", self.frame.size.width); [self ajustIconWith:_iconAlign]; } diff --git a/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftRightViewController.m b/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftRightViewController.m index ea73855..f4fdbba 100644 --- a/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftRightViewController.m +++ b/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftRightViewController.m @@ -16,6 +16,22 @@ @interface LeftRightViewController () @implementation LeftRightViewController +- (void)viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + + // 弹出键盘 +// [self.searchBar becomeFirstResponder]; +} + +- (void)viewDidAppear:(BOOL)animated +{ + [super viewDidAppear:animated]; + + // 弹出键盘 + [self.searchBar becomeFirstResponder]; +} + - (void)viewDidLoad { [super viewDidLoad]; diff --git a/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftViewController.m b/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftViewController.m index 173b37e..8dadb0b 100644 --- a/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftViewController.m +++ b/EVNCustomSearchBarDemo/EVNCustomSearchBarDemo/LeftViewController.m @@ -16,6 +16,14 @@ @interface LeftViewController () @implementation LeftViewController +- (void)viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + + // 弹出键盘 + [self.searchBar becomeFirstResponder]; +} + - (void)viewDidLoad { [super viewDidLoad];