【问题标题】:UISearchBar placeholder text - align to centre programaticallyUISearchBar 占位符文本 - 以编程方式对齐到中心
【发布时间】:2017-11-29 15:22:39
【问题描述】:

我正在使用 UISearchBar 使用 Objective C 进行搜索。我需要以编程方式将搜索栏的占位符文本对齐到搜索栏的中心,并且当用户开始输入时,文本必须向左对齐。

提前致谢

【问题讨论】:

    标签: ios objective-c uisearchbar


    【解决方案1】:

    我以前也遇到过和你一样的问题。我找不到任何解决方法,毕竟我只是使用了UILabelUISearchBarDelegate 方法。

    ViewController.h:

    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UISearchBarDelegate>
    
    @end
    

    ViewController.m:

    #import "ViewController.h"
    
    @interface ViewController () {
        UISearchBar *srchBar;
        UILabel *customPlaceHolderForSearchBar;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        CGRect frm = CGRectMake(0, 0, self.view.frame.size.width, 80);
        srchBar = [[UISearchBar alloc] initWithFrame:frm];
        srchBar.delegate = self;
        [self.view addSubview:srchBar];
    
        customPlaceHolderForSearchBar = [[UILabel alloc] initWithFrame:frm];
        customPlaceHolderForSearchBar.text = @"PlaceHolder text";
        customPlaceHolderForSearchBar.textColor = [UIColor grayColor];
        customPlaceHolderForSearchBar.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:customPlaceHolderForSearchBar];
    
    
    }
    
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
        customPlaceHolderForSearchBar.hidden = YES;
    }
    
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
        if (searchBar.text.length < 1) {
            customPlaceHolderForSearchBar.hidden = NO;
        }
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多