【问题标题】:UISearchBar placeholder aligning and cropping in iOS 7iOS 7 中的 UISearchBar 占位符对齐和裁剪
【发布时间】:2014-01-06 14:21:40
【问题描述】:

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

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

我需要它一直这样。谢谢。

【问题讨论】:

  • 请分享一些代码。此外,请务必向 Apple 提交错误报告。
  • 你需要任何字体大小
  • 你的 uisearchbar 框架尺寸是多少
  • 这与帧大小无关......我猜这是iOS7搜索栏中的错误

标签: iphone objective-c ios7 uisearchbar


【解决方案1】:

新解决方案:

//
//  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];

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

【讨论】:

  • 我不知道如何计算字体的值,因为我的占位符文本是可变的,有时它只有 1 个单词,有时它会填充占位符标签并自动截断为点(...)
  • @JAHelia 这次检查更新的答案比以前更完美
【解决方案2】:
    -(void)viewDidAppear:(BOOL)animated{

        self.searchBar.placeholder=@"woord hier invoeren";

    }


-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

 self.searchBar.placeholder=@"woord hier invoeren.......";

}

【讨论】:

    【解决方案3】:

    我不是在这里给你一个通用的解决方案,但如果你有一个 placeHolder 要添加,最愚蠢和最简单的方法是自己截断 placeHolder,而不是

    searchBar.placeholder = @"wood hier invoeren";

    就这样吧

    searchBar.placeholder = @"word hier invo...";

    我试图弄乱苹果的私有方法但没有运气: searchBar 子视图是: -UISearchBarBackground。 -UISearchBarTextField。 将 UISearchBarBackground 放在一边 UISearchBarTextField 实例的子视图是: -_UISearchBarSearchFieldBackgroundView。 -UIImageView。 -UISearchBarTextFieldLabel。

    我试图弄乱 UISearchBarTextFieldLabel 的矩形(我强调单词 mess,因为这些是私有方法),因为我很确定它的框架在 searchBar 按钮显示时没有正确呈现(书签),如果您选择 searchBar.showsBookmarkButton = NO; placeHolder 文本将按预期截断。 这取决于您,节省一些时间并采用愚蠢的解决方案,但这可以完成任务,或者更深入地研究。 继续努力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 2013-12-21
      • 2017-03-24
      • 1970-01-01
      • 2012-07-08
      • 2014-11-19
      • 2014-02-02
      相关资源
      最近更新 更多