自定义UISearchbar的UITextfield - iOS [英] Customize UITextfield of the UISearchbar - iOS

查看:109
本文介绍了自定义UISearchbar的UITextfield - iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义UISearchbar的文本字段。下图显示了我完成的一半工作。

I'm trying to customize the textfield for the UISearchbar. The picture below shows my half done work.

我有子类UISearchbar并从我的视图控制器调用它。我正试图从文本字段中删除那些深灰色线条。下面是添加到viewcontroller子视图的UISearchbar的实现。

I have subclasses the UISearchbar and called it from my view controller. I'm trying to remove those dark gray lines from the textfield. Below is the implementation of the UISearchbar adding to subview of the viewcontroller.

searchbar = [[SearchBar alloc] initWithFrame:CGRectMake(35,78, 250, 17)];
searchbar.backgroundColor = [UIColor clearColor];
searchbar.layer.borderColor = [[UIColor clearColor] CGColor];
searchbar.layer.borderWidth = 0;

for(UIView *view in searchbar.subviews){
    if([view isKindOfClass:[UITextField class]]){
        UITextField *tf= (UITextField *)view;
        tf.layer.borderColor = [[UIColor clearColor] CGColor];
        tf.delegate = self;
        break;
    }
}
[self.view addSubview:searchbar];
searchbar.delegate = self;

UISearchBar子类:

UISearchBar subclass:

   - (id)initWithFrame:(CGRect)frame
  {
   self = [super initWithFrame:frame];
if (self) {
      // Initialization code
     }
     return self;
}

-(void)layoutSubviews{
     UITextField *searchField;
     [[[self subviews] objectAtIndex:0] removeFromSuperview];
     [self setTintColor:[UIColor clearColor]];
     self.clipsToBounds = YES;
     NSUInteger numViews = [self.subviews count];
     for(int i = 0; i < numViews; i++) {
        if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { 
            searchField = [self.subviews objectAtIndex:i];
             searchField.leftViewMode = UITextFieldViewModeNever;
             searchField.backgroundColor = [UIColor clearColor];

        }


    }
    if(!(searchField == nil)) {            
        searchField.backgroundColor = [UIColor clearColor];
        searchField.textColor = [UIColor blackColor];
        searchField.frame = CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height-10);

        [searchField setBorderStyle:UITextBorderStyleRoundedRect];

    }

    [super layoutSubviews];

}

我正在努力实现这样的目标:
文本字段不应有任何边界。图标是扁平的UIImageView。

I'm trying to achive something like this: The textfield should not have any boundaries. The icons are flattened UIImageView.

推荐答案

这是从UISearchBar子视图层次结构获取文本字段并根据需要设置其属性的简单方法,如

This is an easy way to get textfield from UISearchBar subview hierarchy and set its properties as needed like

  UITextField *txfSearchField = [searchbar valueForKey:@"_searchField"];
[txfSearchField setBackgroundColor:[UIColor whiteColor]];
    [txfSearchField setLeftView:UITextFieldViewModeNever];
    [txfSearchField setBorderStyle:UITextBorderStyleRoundedRect];
    txfSearchField.layer.borderWidth = 8.0f; 
    txfSearchField.layer.cornerRadius = 10.0f;
        txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;

这篇关于自定义UISearchbar的UITextfield - iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆