-iOS培训,iOS学习-------型技术博客、期待与您交流!------------

iOS开发之使用TextField作为搜索框

   
今天给大家带来一个新的技巧,比如平时我们要使用代码创建一个搜索框的时候,一般人都是直接使用系统自带的Searchbar创建,然后设置。今天刚好遇到这个问题,于是我也就屁颠屁颠的去做,结果怎么做都不能显示想要的效果。
最后我想到了一招,能不能使用其他的控件呢,我就想到了TextField,最后真的可以实现。
我想以后在其他的地铁肯定也可以用到,就想到把它封装了一下,希望以后要用的时候能直接拷贝,嘿嘿!

   
   
    UITextField *text = [[UITextField alloc] init];
    text.frame = CGRectMake(0, 0, 320, 30);
    text.background = [UIImage resizeimage:@""];
    //文字垂直居中
    text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //搜索图标
    UIImageView *view = [[UIImageView alloc] init];
    view.image = [UIImage imageWithName:@""];
    view.frame = CGRectMake(0, 0, 35, 35);
    //左边搜索图标的模式
    view.contentMode = UIViewContentModeCenter;
    text.leftView = view;
    //左边搜索图标总是显示
    text.leftViewMode = UITextFieldViewModeAlways;
    //右边删除所有图标
    text.clearButtonMode = UITextFieldViewModeAlways;

    self.navigationItem.titleView = text;

0

 

相关文章:

  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2021-12-26
  • 2022-12-23
  • 2022-02-22
猜你喜欢
  • 2022-12-23
  • 2021-11-23
  • 2021-12-03
  • 2021-11-12
  • 2021-12-10
  • 2022-02-07
相关资源
相似解决方案