gpt4 book ai didi

iphone - iOS 7 中的 UISearchBar 占位符对齐和裁剪

转载 作者:太空狗 更新时间:2023-10-30 03:34:01 25 4
gpt4 key购买 nike

在 iOS 7 中,UISearchbar 占位符居中对齐并覆盖书签按钮,直到搜索栏未被选中:

enter image description here

当它被选中时,它看起来像预期的那样:

enter image description here

我需要它一直看起来像这样。谢谢。

最佳答案

新解决方案:

//
// 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 View 并隐藏现有占位符以提供 searchBar 的真实感觉。当搜索栏激活时再次重新显示实际占位符。

这可能是修复 IOS7 中的 UI 问题的临时 hack。

When Inactive

When Active

旧的解决方案: [searchField setValue:[NSNumber numberWithBool:YES] forKeyPath:@"_placeholderLabel.adjustsFontSizeToFitWidth"];

在 ios7 中不起作用,因为用于显示内容的标签大小足以显示文本,问题是 ios7 的标签宽度错误。它无法重新调整标签宽度。

有一些 hack 可以解决这个问题。

UITextField *searchField = [searchBar valueForKey:@"_searchField"];


UILabel *lable=[searchBar valueForKey:@"_placeholderLabel"];
lable.font=[UIFont fontWithName:lable.font.fontName size:10.0];

根据您自己的搜索栏宽度计算字体大小。我也试图改变特定标签的宽度,但它从来没有奏效。

关于iphone - iOS 7 中的 UISearchBar 占位符对齐和裁剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19257544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com