在 iOS 7 UISearchbar 占位符居中对齐并覆盖书签按钮,直到没有选择搜索栏:
当它被选中时,它看起来像预期的那样:
我需要它一直这样。谢谢你。
在 iOS 7 UISearchbar 占位符居中对齐并覆盖书签按钮,直到没有选择搜索栏:
当它被选中时,它看起来像预期的那样:
我需要它一直这样。谢谢你。
新解决方案:
//
// WPViewController.m
// test
//
// Created by VASANTH K on 02/01/14.
//
//
#import "WPViewController.h"
@interface WPViewController ()
{
UILabel *lableCopy;
}
@end
@implementation WPViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//[self fixSearchBar:searchBar];
// Do any additional setup after loading the view, typically from a nib.
self.searchBar.delegate=self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.searchBar resignFirstResponder];
//[self fixSearchBar:searchBar];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self fixSearchBar:self.searchBar];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)search
{
[self fixSearchBar:self.searchBar];
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[self fixSearchBar:self.searchBar];
}
-(void)fixSearchBar:(UISearchBar*)searchBar
{
UITextField *searchField = [searchBar valueForKey:@"_searchField"];
// [searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
UILabel *lable=[searchField valueForKey:@"_placeholderLabel"];
if(!lableCopy)
{
lableCopy=[[UILabel alloc]initWithFrame:lable.frame];
lableCopy.font=lable.font;
[lableCopy setText:lable.text];
[lableCopy setTextColor:lable.textColor];
UIButton *button;
for (UIView *view in [[[[searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:1] subviews]) {
if([view isKindOfClass:[UIButton class]])
{
button=(UIButton*)view;
break;
}
}
if(button)
{
//lable.hidden=YES;
CGRect newFrame=lable.frame;
newFrame.size.width=button.frame.origin.x-lable.frame.origin.x;
lableCopy.frame=newFrame;
[lableCopy adjustsFontSizeToFitWidth];
//lableCopy.backgroundColor=[UIColor blackColor];
[searchField addSubview:lableCopy];
lableCopy.text=lable.text;
//lableCopy.textColor=[UIColor redColor];
}
}
for (UIView *view in [[searchBar.subviews objectAtIndex:0] subviews]) {
if([view isKindOfClass:[UITextField class]])
{
// NSLog(@"%@",view);
NSLog(@"TextFieldPresent==>%@",view);
if([view isFirstResponder])
{
lable.hidden=NO;
lableCopy.hidden=YES;
}
else
{
lable.hidden=YES;
lableCopy.hidden=NO;
}
break;
}
}
}
@end
此解决方案只是添加新的 UILable 视图并隐藏现有占位符以提供真正的 searchBar 感觉。当搜索栏激活时再次重新显示实际占位符。
这可能是在 IOS7 中修复该 UI 问题的临时破解。
旧解决方案:[searchField setValue:[NSNumber numberWithBool:YES] forKeyPath:@"_placeholderLabel.adjustsFontSizeToFitWidth"];
在 ios7 中不起作用,因为用于显示内容的标签大小足以显示文本,问题是 ios7 的标签宽度错误。它无法重新调整标签宽度。
有一点点破解来解决这个问题。
UITextField *searchField = [searchBar valueForKey:@"_searchField"];
UILabel *lable=[searchBar valueForKey:@"_placeholderLabel"];
lable.font=[UIFont fontWithName:lable.font.fontName size:10.0];
根据您自己的搜索栏宽度计算字体大小。我也试图改变特定标签的宽度,但它从来没有奏效。
-(void)viewDidAppear:(BOOL)animated{
self.searchBar.placeholder=@"woord hier invoeren";
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
self.searchBar.placeholder=@"woord hier invoeren.......";
}
我不是在这里给你一个通用的解决方案,但如果你有一个 placeHolder 要添加,最愚蠢和最简单的方法是自己截断 placeHolder,而不是
searchBar.placeholder = @"wood hier invoeren";
随它去
searchBar.placeholder = @"木头订单...";
我试图弄乱苹果的私有方法,但没有运气:searchBar 子视图是:-UISearchBarBackground。-UISearchBarTextField。将 UISearchBarBackground 放在一边,UISearchBarTextField 实例的子视图是:-_UISearchBarSearchFieldBackgroundView。-UIImageView。-UISearchBarTextFieldLabel。
我所追求的是试图弄乱 UISearchBarTextFieldLabel 的矩形(我强调单词混乱,因为这些是私有方法),因为我很确定它的框架在显示 searchBar 按钮时没有正确呈现(书签) , 如果你选择 searchBar.showsBookmarkButton = NO; placeHolder 文本将按预期截断。这取决于您,节省一些时间并采用愚蠢的解决方案,但这可以完成任务,或者更深入地研究。保持良好的工作。