Подтвердить что ты не робот

Настроить поле UIText в UISearchbar - iOS

Я пытаюсь настроить текстовое поле для UISearchbar. На приведенной ниже картинке показана моя сделанная половина работы.  strong text

У меня есть подклассы UISearchbar и вызывается из моего контроллера. Я пытаюсь удалить эти темные серые линии из текстового поля. Ниже приведена реализация UISearchbar, добавляющая к подзору 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:

   - (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.

enter image description here

4b9b3361

Ответ 1

Это простой способ получить текстовое поле из иерархии subview UISearchBar и установить его свойства по мере необходимости, например

  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;

Ответ 2

Используйте следующее, если вы не хотите использовать недокументированные функции или использовать изображение:

CGSize size = CGSizeMake(30, 30);
// create context with transparent background
UIGraphicsBeginImageContextWithOptions(size, NO, 1);

// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,30,30)
                            cornerRadius:2.0] addClip];
[[UIColor whiteColor] setFill];

UIRectFill(CGRectMake(0, 0, size.width, size.height));
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

[self.searchBar setSearchFieldBackgroundImage:image forState:UIControlStateNormal];

Ответ 3

Начиная с IOS-5 у вас есть внешний прокси-сервер, см. http://developer.apple.com/library/ios/#documentation/uikit/reference/UISearchBar_Class/Reference/Reference.html (есть два раздела под названием "Настройка внешнего вида", проверьте оба).

Здесь рабочий пример, он изменяет все UISearchBars в приложении:

[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"text_box"] forState:UIControlStateNormal];
[[UISearchBar appearance] setImage:[UIImage imageNamed:@"search_icon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
mysearchBar.tintColor = [UIColor whiteColor];

Ответ 4

Реализация с переменным сопротивлением и высокой производительностью

Кому-нибудь, кто читает это и задается вопросом, как легко получить доступ к текстовому полю строки поиска в Swift; вы можете расширить UISearchBar, чтобы добавить свойство textField, используя следующий фрагмент кода, который устойчив к базовым изменениям в реализации и кэширует найденный результат, поэтому он имеет высокую производительность.

import UIKit

private var foundTextFieldAssociationKey = UInt8()

extension UISearchBar {

  var textField: UITextField {
    get {
      let value = objc_getAssociatedObject(self, &foundTextFieldAssociationKey) as? UITextField

      if value == nil {
        let findInView = (UIView) -> UITextField? = { view in
          for subview in view.subviews {
            if let textField = (subview as? UITextField) ?? findInView(subview) {
              return textField
            }
          }
          return nil
        }

        guard let foundTextField = findInView(self) else {
          fatalError("UISearchBar doesn't seem to have a UITextField anywhere in the view hierarchy")
        }

        textField = foundTextField
        return foundTextField
      }

      return value!
    }
    set {
      objc_setAssociatedObject(self, &foundTextFieldAssociationKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
    }
  }

}

Если вы хотите изменить имя свойства, убедитесь, что вы не изменили его на searchField. Это повредит ваш поисковый бар.

Ответ 5

Для тех, кто все еще ищет ответ, Apple добавила свойство searchTextField в UISearchBar в iOS 13. searchTextField - это UISeachTextField, который наследуется от UITextField.

let searchBar = UISearchBar()
var searchField : UITextField
if #available(iOS 13.0, *) {
    searchField = searchBar.searchTextField
} else {
    searchField = //One of the other methods listed
}

Ответ 6

Я нашел для iOS 10 Мне нужно сделать это (заимствовано сверху и адаптировано быстро)

extension UISearchBar {
    var textField: UITextField? {

    func findInView(_ view: UIView) -> UITextField? {
        for subview in view.subviews {
            print("checking \(subview)")
            if let textField = subview as? UITextField {
                return textField
            }
            else if let v = findInView(subview) {
                return v
            }
        }
        return nil
      }

      return findInView(self)
    }
 }  

Ответ 7

Короткие и простые

extension UISearchBar {
    var textField:UITextField {
        guard let txtField = self.value(forKey: "_searchField") as? UITextField else {
            assertionFailure()
            return UITextField()
        }
        return txtField
    }
}

Ответ 8

Теперь в iOS 13 у вас есть свойство searchTextField для прямого доступа к текстовому полю.